編程方式取得Spring上下文的Properties


spring初始化時,可以使用Properties配置器把properties文件裝載到Spring的上下文中。

 

Xml代碼   收藏代碼
  1. ...  
  2. xmlns:context="http://www.springframework.org/schema/context"  
  3.   
  4. xsi:schemaLocation=“http://www.springframework.org/schema/context  
  5.                 http://www.springframework.org/schema/context/spring-context-3.0.xsd”  
  6.   
  7. ...  
  8.   
  9. <context:property-placeholder location="classpath:dataSource.properties" />  

 

這樣在Spring的配置文件中可以用表達式來獲得load進來的properties內容,例如:

 

Xml代碼   收藏代碼
  1. <property name="url" value="${url}" />  
  2. <property name="username" value="${username}" />  
  3. <property name="password" value="${password}" />  

 

有時候我們在程序中也需要用到這些配置,那么如何取值,顯然不能使用${}方式的。

這時要決定用什么方式來獲取properties了,最方便的當然是直接讀取文件,此處省略。

如果程序一定要用通過Spring加載的properties,那么我們首先要得到Context了。

 

1、FileSystemXmlApplicationContext——從指定的目錄中加載:

 

Java代碼   收藏代碼
  1. ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");  

 

2、ClassPathXmlApplicationContext——從classpath路徑加載:

Java代碼   收藏代碼
  1. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  

 

3、參照http://blog.csdn.net/dqatsh/article/details/3469278, 通過web.xml來獲取Context。

 

4、在servlet中獲取。

 

Java代碼   收藏代碼
  1. ServletContext servletContext = servlet.getServletContext();     
  2. WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);  

 

然后可以通過相應的bean去訪問需要的properties(spring配置文件中${}方式設置到bean里)的值,這里不記錄。

 

 

用PropertyPlaceholderConfigurer在加載上下文的時候暴露properties

 

Java代碼   收藏代碼
  1. <bean id="configBean"   
  2.  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   
  3.             <property name="location">   
  4.                 <value>hello.properties</value>   
  5.             </property>   
  6. </bean>   

 

表明PropertyPlaceholderConfigurer是承擔properties讀取任務的類。

 

下面的類繼承PropertyPlaceholderConfigurer,通過重寫processProperties方法把properties暴露出去了。

 

Java代碼   收藏代碼
  1. import java.util.HashMap;  
  2. import java.util.Map;  
  3. import java.util.Properties;  
  4.   
  5. import org.springframework.beans.BeansException;  
  6. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;  
  7. import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;  
  8.   
  9. public class CustomizedPropertyConfigurer extends PropertyPlaceholderConfigurer {  
  10.   
  11.     private static Map<String, Object> ctxPropertiesMap;  
  12.   
  13.     @Override  
  14.     protected void processProperties(ConfigurableListableBeanFactory beanFactory,  
  15.             Properties props)throws BeansException {  
  16.   
  17.         super.processProperties(beanFactory, props);  
  18.         //load properties to ctxPropertiesMap  
  19.         ctxPropertiesMap = new HashMap<String, Object>();  
  20.         for (Object key : props.keySet()) {  
  21.             String keyStr = key.toString();  
  22.             String value = props.getProperty(keyStr);  
  23.             ctxPropertiesMap.put(keyStr, value);  
  24.         }  
  25.     }  
  26.   
  27.     //static method for accessing context properties  
  28.     public static Object getContextProperty(String name) {  
  29.         return ctxPropertiesMap.get(name);  
  30.     }  
  31. }  

 

這樣此類即完成了PropertyPlaceholderConfigurer的任務,同時又提供了上下文properties訪問的功能。

於是在Spring配置文件中把PropertyPlaceholderConfigurer改成CustomizedPropertyConfigurer

 

Xml代碼   收藏代碼
  1. <!-- use customized properties configurer to expose properties to program -->  
  2. <bean id="configBean"   
  3.     class="com.payment.taobaoNavigator.util.CustomizedPropertyConfigurer">  
  4.     <property name="location" value="classpath:dataSource.properties" />  
  5. </bean>  
  6. 如果有多個配置文件就要這樣寫:
  7. <bean id="configBean"
  8.   class="com.payment.taobaoNavigator.util.CustomizedPropertyConfigurer">
  9.    <property name="locations">
  10.      <list>
  11.          <value>classpath:文件1.properties </value>
  12.          <value>classpath:文件2.properties </value>
  13.          <value>classpath:文件3.properties </value>
  14.       </list>
  15.    </property>
  16. </bean>

 

 

 

最后在程序中我們便可以使用CustomizedPropertyConfigurer.getContextProperty()來取得上下文中的properties的值了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM