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

 

在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>

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值