编程方式取得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>
- 如果有多个配置文件就要这样写:
- <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的值了。
编程方式取得Spring上下文的Properties的更多相关文章
- Spring加载properties文件的两种方式
在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...
- spring boot application.properties 属性详解
2019年3月21日17:09:59 英文原版: https://docs.spring.io/spring-boot/docs/current/reference/html/common-appli ...
- 以编程方式使用 Microsoft Office Visio 2003 ActiveX 控件
以编程方式使用 Microsoft Office Visio 2003 ActiveX 控件 2007/10/29 Mark BukovecEmpire Down Development 适用于:Mi ...
- spring boot application.properties 配置参数详情
multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...
- 【转】spring boot application.properties 配置参数详情
multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...
- 大厂面试官最常问的@Configuration+@Bean(JDKConfig编程方式)
大厂面试官最常问的@Configuration+@Bean(JDKConfig编程方式) 现在大部分的Spring项目都采用了基于注解的配置,采用了@Configuration 替换标签的做法.一 ...
- SpringBoot 2.0 编程方式配置,不使用默认配置方式
SpringBoot的一般配置是直接使用application.properties或者application.yml,因为SpringBoot会读取.perperties和yml文件来覆盖默认配置: ...
- ASP.NET MVC下的四种验证编程方式
ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表,但是在真正执行目标Action方法之前,还需要对绑定的参数实施验证以确保其有效性,我们将针对参数的验证成为Model绑定 ...
- ASP.NET MVC下的四种验证编程方式【转】
ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表,但是在真正执行目标Action方法之前,还需要对绑定的参数实施验证以确保其有效 性,我们将针对参数的验证成为Model绑 ...
随机推荐
- rsync故障排查整理
1.客户端的错误现象:No route to host rsync服务端开启了iptables防火墙 rsync -avz /etc/hosts rsync_backup@172.16.1.41::b ...
- 某考试 T1 function
(数据范围 n<=10^9 ,T<=10 ) 首先,我来证明一下 Σμ(d) * σ(i/d)^2 = σ(i^2) 相信做过约数个数和的童鞋都可以完成从右式推到左式,那么我现在就说一下怎 ...
- Yelp面试题目
题目:FizzBuzz 从stdin得到数字N(<10^7),然后从打印出从1到N的数字.输出到stdout,假设数字是3的倍数的话就仅仅打印"Buzz",假设数字是5的倍数 ...
- mybatis一对多
mapper.xml <mapper namespace="com.oracle.dao.one2manyDao"> <resultMap type=" ...
- u-boot简单学习笔记(一)
一:Bootloader启动结构: 由于 Boot Loader 的实现依赖于 CPU 的体系结构,因此大多数 Boot Loader 都分为 stage1 和 stage2 两大部分.依赖 ...
- Liunx之Lamp搭建笔记
1:LAMP源代码搭建用户关系 a. apache服务以daemon用户的处理请求.以root身份作为主进程. b. php源代码安装,会在httpd.conf文件里自己主动增加调用模块.可是在该文 ...
- Ctrl+Enter 选中文本提交
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <bod ...
- 在vc6.0下编的对话框界面如果没做过其他处理,往往显的很生硬,怎么样才能使他有Windows XP的风格呢,其实也很简单,我们来看看下面两种方法。
在vc6.0下编的对话框界面如果没做过其他处理,往往显的很生硬,怎么样才能使他有Windows XP的风格呢,其实也很简单,我们来看看下面两种方法. 方法一: 1.首先确认你在Windows ...
- 创建Vue项目的步骤
第一步: 对于要创建项目的工作目录,要先进性管理,命令:npm init -y 第二步: 初始化webpack 包,命令:vue init webpack 自定义名称 第三步: 在components ...
- python day- 6 is 和 ==的区别 encode 和 decode
1.is 和 == 的区别. == 是由来判断左右两边的内容是否相等. is 是用来判断内存地址是否相同. 引进 id ( )函数 小数据池: 对于字符串 ,数字 ,bool 值进行 id()计 ...