如何使用@Value和@PropertySource注入外部資源
1、簡(jiǎn)介
在Spring Boot進(jìn)行項(xiàng)目開發(fā)的過(guò)程中,肯定會(huì)有這樣一種場(chǎng)景,比如說(shuō)事件上報(bào),在開發(fā)時(shí)開發(fā)人員可能會(huì)模擬在代碼中寫入一個(gè)事件上報(bào)Url,然后當(dāng)部署到生產(chǎn)環(huán)境時(shí),該url就需要從外部導(dǎo)入,一般通過(guò)修改配置文件的方式達(dá)到類似的目的。
在Spring開發(fā)中經(jīng)常涉及調(diào)用各種資源的情況,包含普通文件,網(wǎng)址,配置文件,系統(tǒng)環(huán)境變量等,這種情況可以使用Spring EL-Spring表達(dá)式語(yǔ)言實(shí)現(xiàn)資源的注入。
2、實(shí)踐
程序演示使用IDEA集成開發(fā)環(huán)境,演示@Value的使用,并通過(guò)注解@PropertySource可以注入自定義配置文件或者其他任意新建的文本文件。
注意:以下實(shí)踐在Spring環(huán)境下是通用的,Spring Boot也是可用的的。
2.1項(xiàng)目結(jié)構(gòu)
2.2 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>value</artifactId> <version>0.0.1-SNAPSHOT</version> <name>value</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
其中
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency>
依賴可以簡(jiǎn)化文件相關(guān)的操作,本例中使用commons-io將file轉(zhuǎn)換成字符串。
2.3 DemoService
package com.example.value.service; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; /** * 需被注入的類 * * @Owner: * @Time: 2019/3/31-12:35 */ @Service public class DemoService { @Value("其他類的屬性") private String another; public String getAnother() { return another; } public void setAnother(String another) { this.another = another; } }
其中在上類中使用@Value注解注入了普通字符串“其他類的屬性?!?/p>
2.4 ElConfig 類
package com.example.value.config; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.core.env.Environment; import org.springframework.core.io.Resource; /** * 演示@Value的使用 * * @Owner: * @Time: 2019/3/31-12:32 */ @Configuration @ComponentScan("com.example.value.service") //掃包 @PropertySource("classpath:test.properties") //注意文件格式的指定 public class ElConfig { @Value("I Love You!") //1 注入普通字符串 private String normal; @Value("#{systemProperties['os.name']}") //2 注入操作系統(tǒng)屬性 private String osName; @Value("#{ T(java.lang.Math).random() * 100.0 }") //3 注入表達(dá)式結(jié)果 private double randomNumber; @Value("#{demoService.another}") //4 注入其他Bean的屬性 private String fromAnother; @Value("classpath:test.txt") //5 注入了文件資源 private Resource testFile; @Value("http://www.baidu.com") //6 注入網(wǎng)頁(yè)資源 private Resource testUrl; @Value("${book.name}") //7 注入classpath:test.properties中資源項(xiàng),注意美元符號(hào)$ private String bookName; @Autowired private Environment environment; //7 屬性也可以從environment中獲取。 @Bean //7 public static PropertySourcesPlaceholderConfigurer propertyConfigure() { return new PropertySourcesPlaceholderConfigurer(); } public void outputResource() { try { System.out.println(normal); System.out.println(osName); System.out.println(randomNumber); System.out.println(fromAnother); System.out.println(IOUtils.toString(testFile.getInputStream())); System.out.println(IOUtils.toString(testUrl.getInputStream())); System.out.println(bookName); System.out.println(environment.getProperty("book.author")); System.out.println(environment.getProperty("book.school")); } catch (Exception e) { e.printStackTrace(); } } }
注意:注入配置配件使用@PropertySource指定文件地址,若使用@Value注入,則要配置一個(gè)PropertySourcePlaceholderConfigure的Bean
2.5 ValueApplication主類
package com.example.value; import com.example.value.config.ElConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @SpringBootApplication public class ValueApplication { @Autowired private ElConfig config; public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ElConfig.class); ElConfig resourceService = context.getBean(ElConfig.class); resourceService.outputResource(); } }
2.6 resources下兩個(gè)文件的內(nèi)容
2.6.1 test.txt
123334455 aaa bbb ccc
2.6.2 test.properties
book.author=sqh book.name=spring boot book.school=NJUST
3、控制臺(tái)打印結(jié)果
I Love You! #直接注入字符串
Windows 8.1 #注入了系統(tǒng)屬性
87.12913167952843 # 注入了表達(dá)式結(jié)果
其他類的屬性 # 其他類的屬性
123334455 # test.txt
aaa
bbb
ccc
<!DOCTYPE html> # 注入了網(wǎng)頁(yè)內(nèi)容
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地圖</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>視頻</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>貼吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登錄</a> </noscript> <script>document.write('<a + encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登錄</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多產(chǎn)品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關(guān)于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必讀</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a> 京ICP證030173號(hào) <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>spring boot # 配置文件中的三個(gè)值
sqh
NJUST
4、總結(jié)
通過(guò)上述的方式,也附帶的闡述了在Spring Boot中不使用application.properties配置文件,而是用其他任意的配置文件來(lái)放入程序配置的使用方法,可以看到@Value很靈活,也很方便,作者暫時(shí)比較常用的就是在applicaiton.properties中放入類似book.name這樣的配置項(xiàng)而使用@Value("${book.name}")直接注入的方式。
Spring的@PropertySource和@Value注解例子
在這篇文章中,我們會(huì)利用Spring的@PropertySource和@Value兩個(gè)注解從配置文件properties中讀取值,以及如何從配置文件中的值轉(zhuǎn)換為L(zhǎng)ist對(duì)象。
創(chuàng)建Spring配置Class
@Configurable @ComponentScan(basePackages = "com.9leg.java.spring") @PropertySource(value = "classpath:spring/config.properties") public class AppConfigTest { @Bean public PropertySourcesPlaceholderConfigurer propertyConfigInDev() { return new PropertySourcesPlaceholderConfigurer(); } }
通過(guò)@PropertySource注解將properties配置文件中的值存儲(chǔ)到Spring的 Environment中,Environment接口提供方法去讀取配置文件中的值,參數(shù)是properties文件中定義的key值。上面是讀取一個(gè)配置文件,如果你想要讀取多個(gè)配置文件,請(qǐng)看下面代碼片段:
@PropertySource(value = {"classpath:spring/config.properties","classpath:spring/news.properties"})
在Spring 4版本中,Spring提供了一個(gè)新的注解——@PropertySources,從名字就可以猜測(cè)到它是為多配置文件而準(zhǔn)備的。
@PropertySources({ @PropertySource("classpath:config.properties"), @PropertySource("classpath:db.properties") }) public class AppConfig { //something }
另外在Spring 4版本中,@PropertySource允許忽略不存在的配置文件。先看下面的代碼片段:
@Configuration @PropertySource("classpath:missing.properties") public class AppConfig { //something }
如果missing.properties不存在或找不到,系統(tǒng)則會(huì)拋出異常FileNotFoundException。
Caused by: java.io.FileNotFoundException:
class path resource [missiong.properties] cannot be opened because it does not exist
幸好Spring 4為我們提供了ignoreResourceNotFound屬性來(lái)忽略找不到的文件
@Configuration @PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true) public class AppConfig { //something } @PropertySources({ @PropertySource(value = "classpath:missing.properties", ignoreResourceNotFound=true), @PropertySource("classpath:config.properties") })
最上面的AppConfigTest的配置代碼等于如下的XML配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.9leg.java.spring"/> <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="ignoreUnresolvablePlaceholders" value="true"/> <property name="locations"> <list> <value>classpath:spring/config.properties</value> </list> </property> </bean> </beans>
創(chuàng)建properties配置文件
server.name=9leg,spring server.id=10,11,12 server.jdbc=com.mysql.jdbc.Driver
創(chuàng)建一個(gè)簡(jiǎn)單的服務(wù)
@Component(value = "mockConfigTest") public class MockConfigTest { @Value("#{'${server.name}'.split(',')}") private List<String> servers; @Value("#{'${server.id}'.split(',')}") private List<Integer> serverId; @Value("${server.host:127.0.0.1}") private String noProKey; @Autowired private Environment environment; public void readValues() { System.out.println("Services Size : " + servers.size()); for(String s : servers) System.out.println(s); System.out.println("ServicesId Size : " + serverId.size()); for(Integer i : serverId) System.out.println(i); System.out.println("Server Host : " + noProKey); String property = environment.getProperty("server.jdbc"); System.out.println("Server Jdbc : " + property); } public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(AppConfigTest.class); MockConfigTest mockConfigTest = (MockConfigTest) annotationConfigApplicationContext.getBean("mockConfigTest"); mockConfigTest.readValues(); } }
首先使用@Component注解聲明,接著就是屬性字段上的@Value注解,但在這里比較特殊,是通過(guò)split()方法,將配置文件中的9leg,spring分割后組成list對(duì)象。心細(xì)的同學(xué)可能注意到了server.host這個(gè)key并不存在配置文件中。是的,在這里使用的是默認(rèn)值,即127.0.0.1,它的格式是這樣的。
@value("${key:default}") private String var;
然后注入了Environment,可以通過(guò)getProperty(key)來(lái)獲取配置文件中的值。 運(yùn)行main方法,來(lái)看下輸出結(jié)果:
Services Size : 2
9leg
spring
ServicesId Size : 3
10
11
12
Server Host : 127.0.0.1
Server Jdbc : com.mysql.jdbc.Driver
最后要說(shuō)一點(diǎn),在main方法中請(qǐng)使用
new AnnotationConfigApplicationContext(AppConfigTest.class)
來(lái)代替
new ClassPathXmlApplicationContext(“applicationContext.xml”)
或者
new FileSystemXmlApplicationContext(“src/main/resources/applicationContext.xml”)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring cloud consul使用ip注冊(cè)服務(wù)的方法示例
這篇文章主要介紹了spring cloud consul使用ip注冊(cè)服務(wù)的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Java 基礎(chǔ)語(yǔ)法讓你弄懂類和對(duì)象
C 語(yǔ)言是面向過(guò)程的,而 Java 是面向?qū)ο笫俏覀兂B牭降囊痪湓?,這章將帶你揭曉Java 基礎(chǔ)語(yǔ)法中類與對(duì)象到底是什么,需要的朋友請(qǐng)參考下文2021-08-08MyBatis中#號(hào)與美元符號(hào)的區(qū)別
#{變量名}可以進(jìn)行預(yù)編譯、類型匹配等操作,#{變量名}會(huì)轉(zhuǎn)化為jdbc的類型。很多朋友不清楚在mybatis中#號(hào)與美元符號(hào)的不同,接下來(lái)通過(guò)本文給大家介紹兩者的區(qū)別,感興趣的朋友參考下吧2017-01-01Mybatis Select Count(*)的返回值類型介紹
這篇文章主要介紹了Mybatis Select Count(*)的返回值類型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Java中的UrlDecoder 和 UrlEncoder_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
HTML 格式編碼的實(shí)用工具類。該類包含了將 String 轉(zhuǎn)換為 application/x-www-form-urlencoded MIME 格式的靜態(tài)方法。下文通過(guò)實(shí)例代碼給大家介紹Java中的UrlDecoder 和 UrlEncoder知識(shí),感興趣的的朋友一起看看吧2017-07-07Spring+SpringMVC+MyBatis整合詳細(xì)教程(SSM)
Spring是一個(gè)開源框架,Spring是于2003 年興起的一個(gè)輕量級(jí)的Java 開發(fā)框架。這篇文章主要介紹了Spring+SpringMVC+MyBatis整合詳細(xì)教程(SSM),需要的朋友可以參考下2017-10-10Springboot?application.yml配置文件拆分方式
這篇文章主要介紹了Springboot?application.yml配置文件拆分方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05