编程方式取得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绑 ...
随机推荐
- CodeForces 438D 线段树 剪枝
D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...
- Laravel 之Auth用户认证
(1)生成Auth所需文件 打开phpstorm的命令行: php artisan make:auth 生成成功后,打开web.php, 发现多了如下代码: Auth::routes(); Route ...
- 读取编码器信息Python2.7和Python3.3版本差异及解决,一次订阅多次调用callback的解决
1. Python3.3以字节类型返回编码器信息,b'...',BUF: b'\xc3KOO\x00OO\x00OO\x00OO\x00OO\x00\x03\x00\x00\x00\x00\x99R\ ...
- 使用CSS去除 去掉超链接的下划线方法
我们可以用CSS语法来控制超链接的形式.颜色变化,为什么链接一定要使用下划线和颜色区分呢? 其主要原因主要是考虑到 1.视力差的人 2.色盲的人 ... 下面我们做一个这样的链接:未被点击时超链接 ...
- Python机器学习--回归
线性回归 # -*- coding: utf-8 -*- """ Created on Wed Aug 30 19:55:37 2017 @author: Adminis ...
- [Testing] Config jest to test Javascript Application -- Part 3
Run Jest Watch Mode by default locally with is-ci-cli In CI, we don’t want to start the tests in wat ...
- hadoop优质链接
http://wiki.apache.org/hadoop/FAQ
- [BLE]CC2640之ADC功能实现和供电电压的採集
一.开篇 Write programs that do one thing and do it well ~~~~~ 发现非常多人关于使用CC2640/CC2650的过程中比較难以应对的问题就是实现A ...
- Android——ListView优化
1.ListView基本概念 列表显示需要三个元素: ListView:用来展示列表的View. 适配器:用来把数据映射到ListView上 数据:具体的将被映射的字符串,图片或基本组件 适配器类型分 ...
- HDOJ 1217 Arbitrage(拟最短路,floyd算法)
Arbitrage Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...