在spring初始化時,可以使用Properties配置器把properties文件裝載到Spring的上下文中。
- ...
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation=“http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd”
- ...
- <context:property-placeholder location="classpath:dataSource.properties" />
這樣在Spring的配置文件中可以用表達式來獲得load進來的properties內容,例如:
- <property name="url" value="${url}" />
- <property name="username" value="${username}" />
- <property name="password" value="${password}" />
有時候我們在程序中也需要用到這些配置,那么如何取值,顯然不能使用${}方式的。
這時要決定用什么方式來獲取properties了,最方便的當然是直接讀取文件,此處省略。
如果程序一定要用通過Spring加載的properties,那么我們首先要得到Context了。
1、FileSystemXmlApplicationContext——從指定的目錄中加載:
- ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
2、ClassPathXmlApplicationContext——從classpath路徑加載:
- ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
3、參照http://blog.csdn.net/dqatsh/article/details/3469278, 通過web.xml來獲取Context。
4、在servlet中獲取。
- ServletContext servletContext = servlet.getServletContext();
- WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
然后可以通過相應的bean去訪問需要的properties(spring配置文件中${}方式設置到bean里)的值,這里不記錄。
用PropertyPlaceholderConfigurer在加載上下文的時候暴露properties
- <bean id="configBean"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location">
- <value>hello.properties</value>
- </property>
- </bean>
表明PropertyPlaceholderConfigurer是承擔properties讀取任務的類。
下面的類繼承PropertyPlaceholderConfigurer,通過重寫processProperties方法把properties暴露出去了。
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Properties;
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
- import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
- public class CustomizedPropertyConfigurer extends PropertyPlaceholderConfigurer {
- private static Map<String, Object> ctxPropertiesMap;
- @Override
- protected void processProperties(ConfigurableListableBeanFactory beanFactory,
- Properties props)throws BeansException {
- super.processProperties(beanFactory, props);
- //load properties to ctxPropertiesMap
- ctxPropertiesMap = new HashMap<String, Object>();
- for (Object key : props.keySet()) {
- String keyStr = key.toString();
- String value = props.getProperty(keyStr);
- ctxPropertiesMap.put(keyStr, value);
- }
- }
- //static method for accessing context properties
- public static Object getContextProperty(String name) {
- return ctxPropertiesMap.get(name);
- }
- }
這樣此類即完成了PropertyPlaceholderConfigurer的任務,同時又提供了上下文properties訪問的功能。
於是在Spring配置文件中把PropertyPlaceholderConfigurer改成CustomizedPropertyConfigurer
- <!-- use customized properties configurer to expose properties to program -->
- <bean id="configBean"
- class="com.payment.taobaoNavigator.util.CustomizedPropertyConfigurer">
- <property name="location" value="classpath:dataSource.properties" />
- </bean>
- 如果有多個配置文件就要這樣寫:
- <bean id="configBean"
- class="com.payment.taobaoNavigator.util.CustomizedPropertyConfigurer">
- <property name="locations">
- <list>
- <value>classpath:文件1.properties </value>
- <value>classpath:文件2.properties </value>
- <value>classpath:文件3.properties </value>
- </list>
- </property>
- </bean>
最后在程序中我們便可以使用CustomizedPropertyConfigurer.getContextProperty()來取得上下文中的properties的值了。

