在項目中如果有些參數經常需要修改,或者後期可能需要修改,那我們最好把這些參數放到properties檔案中,源代碼中讀取properties裡面的配置,這樣後期隻需要改動properties檔案即可,不需要修改源代碼,這樣更加友善。在spring中也可以這麼做,而且Spring有兩種加載properties檔案的方式:基于xml方式和基于注解方式。下面分别讨論下這兩種方式。
1. 通過xml方式加載properties檔案
我們以Spring執行個體化dataSource為例,我們一般會在beans.xml檔案中進行如下配置:
[html] view plain copy
- <!-- com.mchange.v2.c3p0.ComboPooledDataSource類在c3p0-0.9.5.1.jar包的com.mchange.v2.c3p0包中 -->
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="com.mysql.jdbc.Driver" />
- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/shop" />
- <property name="user" value="root" />
- <property name="password" value="root" />
- </bean>
現在如果我們要改變dataSource,我們就得修改這些源代碼,但是我們如果使用properties檔案的話,隻需要修改那裡面的即可,就不管源代碼的東西了。那麼如何做呢?
Spring中有個<context:property-placeholder location=""/>标簽,可以用來加載properties配置檔案,location是配置檔案的路徑,我們現在在工程目錄的src下建立一個conn.properties檔案,裡面寫上上面dataSource的配置:
[plain] view plain copy
- dataSource=com.mchange.v2.c3p0.ComboPooledDataSource
- driverClass=com.mysql.jdbc.Driver
- jdbcUrl=jdbc\:mysql\://localhost\:3306/shop
- user=root
- password=root
現在隻需要在beans.xml中做如下修改即可:
[html] view plain copy
- <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>
<context:property-placeholder location=""/>标簽也可以用下面的<bean>标簽來代替,<bean>标簽我們更加熟悉,可讀性更強:
[html] view plain copy
- <!-- 與上面的配置等價,下面的更容易了解 -->
- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations"> <!-- PropertyPlaceholderConfigurer類中有個locations屬性,接收的是一個數組,即我們可以在下面配好多個properties檔案 -->
- <array>
- <value>classpath:conn.properties</value>
- </array>
- </property>
- </bean>
雖然看起來沒有上面的<context:property-placeholder location=""/>簡潔,但是更加清晰,建議使用後面的這種。但是這個隻限于xml的方式,即在beans.xml中用${key}擷取配置檔案中的值value。
2. 通過注解方式加載properties檔案
還有一種就是通過注解的方式,在Java代碼中使用@Value注解來加載配置檔案中的值。
我們來看一個例子:假如我們要在程式中擷取某個檔案的絕對路徑,我們很自然會想到不能在程式中寫死,那麼我們也可以解除安裝properties檔案中。還是在src目錄下建立一個public.properties檔案,假設裡面寫了一條記錄:
[plain] view plain copy
- filePath=E\:\\web\\apache-tomcat-8.0.26\\webapps\\E_shop\\image
如果想在java代碼中通過注解來擷取這個filePath的話,首先得在beans.xml檔案中配置一下注解的方式:
[html] view plain copy
- <!-- 第二種方式是使用注解的方式注入,主要用在java代碼中使用注解注入properties檔案中相應的value值 -->
- <bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
- <property name="locations"><!-- 這裡是PropertiesFactoryBean類,它也有個locations屬性,也是接收一個數組,跟上面一樣
- <array>
- <value>classpath:public.properties</value>
- </array>
- </property>
- </bean>
現在我們可以在java代碼中使用注解來擷取filePath的值了:
[java] view plain copy
- @Component("fileUpload")
- public class FileUploadUtil implements FileUpload {
- private String filePath;
- @Value("#{prop.filePath}")
- //@Value表示去beans.xml檔案中找id="prop"的bean,它是通過注解的方式讀取properties配置檔案的,然後去相應的配置檔案中讀取key=filePath的對應的value值
- public void setFilePath(String filePath) {
- System.out.println(filePath);
- this.filePath = filePath;
- }
注意要有set方法才能被注入進來,注解寫在set方法上即可。在setFilePath方法中通過控制台列印filePath是為了在啟動tomcat的時候,觀察控制台有沒有輸出來,如果有,說明Spring在啟動時,已經将filePath給加載好了,我們看一下控制台的啟動資訊:

以上就是Spring加載properties配置檔案的兩種方式。實際上,上面基于xml方式中的PropertyPlaceholderConfigurer類和這裡基于注解方式的PropertiesFactoryBean類都是繼承PropertiesLoaderSupport,都是用來加載properties配置檔案的。
原文:http://blog.csdn.net/eson_15/article/details/51365707 感謝作者!