spring加载配置文件
在項目中有些參數經常需要修改,或者后期可能會有改動時,那我們最好把這些參數放到properties文件中,在源代碼中讀取properties里面的配置,這樣后期只需要改動properties文件即可,不需要修改源碼。下面討論spring兩種加載方式,基于xml和基于注解的加載方式。
1. 通過xml方式加載properties文件
以Spring實例化dataSource為例,先在工程目錄的src下新建一個conn.properties文件,里面寫上上面dataSource的配置:
dataSource=com.mchange.v2.c3p0.ComboPooledDataSource driverClass=com.mysql.jdbc.Driver jdbcUrl=jdbc:mysql://localhost:3306/shop username=root password=root然后在只需要在beans.xml中做如下修改即可:
<context:property-placeholder location="classpath:conn.properties"/><!-- 加載配置文件 --> <!-- com.mchange.v2.c3p0.ComboPooledDataSource類在c3p0-0.9.5.1.jar包的com.mchange.v2.c3p0包中 --> <bean id="dataSource" class="${dataSource}"> <!-- 這些配置Spring在啟動時會去conn.properties中找 --> <property name="driverClass" value="${driverClass}" /> <property name="jdbcUrl" value="${jdbcUrl}" /> <property name="user" value="${user}" /> <property name="password" value="${password}" /> </bean>標簽也可以用下面的標簽來代替,可讀性更強:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <!-- PropertyPlaceholderConfigurer類中有個locations屬性,接收的是一個數組,即我們可以在下面配好多個properties文件 --> <array> <value>classpath:conf.properties</value> </array> </property> </bean>2. 通過注解方式加載properties文件
首先新建一個資源文件:public.properties
shop.url=http://magic/shop第一種配置方式:
<!-- 確保可在@Value中, 使用SeEL表達式獲取資源屬性 --><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="properties" ref="configProperties" /></bean><bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"><property name="locations"><list><value>classpath:config/*.properties</value></list></property></bean>在java代碼中用@Value獲取配置屬性值
@Value("${shop.url}")private String url;還有一種方式更簡潔:
<bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="fileEncoding" value="UTF-8"/> <property name="locations"><!-- 這里是PropertiesFactoryBean類,它也有個locations屬性,也是接收一個數組,跟上面一樣 <array> <value>classpath:public.properties</value> </array> </property> </bean> <!--或者--><context:property-placeholder location="classpath:public.properties" /> //注意,這種表達式要有set方法才能被注入進來,注解寫在set方法上即可private String url; @Value("#{prop.shop.url}") //@Value表示去beans.xml文件中找id="prop"的bean,它是通過注解的方式讀取properties配置文件的,然后去相應的配置文件中讀取key=shop.url的對應的value值 public void setUrl(String url) { this.token= url; }3.通過 @PropertySource和@Value 來讀取配置文件
舉個栗子:
@Component @PropertySource(value = {"classpath:common.properties", "classpath:abc.properties"}) public class Configs {@Value("${connect.api.apiKeyId}")public String apiKeyId;@Value("${connect.api.secretApiKey}")public String secretApiKey;public String getApiKeyId() {return apiKeyId;}public String getSecretApiKey() {return secretApiKey;} }我們來具體分析下:
1、@Component注解說明這是一個普通的bean,在Component Scanning時會被掃描到并被注入到Bean容器中;我們可以在其它引用此類的地方進行自動裝配。@Autowired這個注解表示對這個bean進行自動裝配。 比如:
@Controller public class HomeController {@Autowiredprivate Configs configs; }2、@PropertySource注解用來指定要讀取的配置文件的路徑從而讀取這些配置文件,可以同時指定多個配置文件;
3、@Value("${connect.api.apiKeyId}")用來讀取屬性key=connect.api.apiKeyId所對應的值并把值賦值給屬性apiKeyId;
后續會補充static屬性的注入方式,期待吧!!!
附:關于配置文件加載問題
轉載于:https://www.cnblogs.com/lyjing/p/8406827.html
總結
以上是生活随笔為你收集整理的spring加载配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 民族团结标语文案30句
- 下一篇: stream pipe的原理及简化源码分