在spring初始化时,可以使用Properties配置器把properties文件装载到Spring的上下文中。

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

这样在Spring的配置文件中可以用表达式来获得load进来的properties内容,例如:

  1. <property name="url" value="${url}" />
  2. <property name="username" value="${username}" />
  3. <property name="password" value="${password}" />

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

这时要决定用什么方式来获取properties了,最方便的当然是直接读取文件,此处省略。

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

1、FileSystemXmlApplicationContext——从指定的目录中加载:

  1. ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");

2、ClassPathXmlApplicationContext——从classpath路径加载:

  1. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

3、参照http://blog.csdn.net/dqatsh/article/details/3469278, 通过web.xml来获取Context。

4、在servlet中获取。

  1. ServletContext servletContext = servlet.getServletContext();
  2. WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

然后可以通过相应的bean去访问需要的properties(spring配置文件中${}方式设置到bean里)的值,这里不记录。

用PropertyPlaceholderConfigurer在加载上下文的时候暴露properties

  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暴露出去了。

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

这样此类即完成了PropertyPlaceholderConfigurer的任务,同时又提供了上下文properties访问的功能。

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

  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的值了。

编程方式取得Spring上下文的Properties的更多相关文章

  1. Spring加载properties文件的两种方式

    在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...

  2. spring boot application.properties 属性详解

    2019年3月21日17:09:59 英文原版: https://docs.spring.io/spring-boot/docs/current/reference/html/common-appli ...

  3. 以编程方式使用 Microsoft Office Visio 2003 ActiveX 控件

    以编程方式使用 Microsoft Office Visio 2003 ActiveX 控件 2007/10/29 Mark BukovecEmpire Down Development 适用于:Mi ...

  4. spring boot application.properties 配置参数详情

    multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...

  5. 【转】spring boot application.properties 配置参数详情

    multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...

  6. 大厂面试官最常问的@Configuration+@Bean(JDKConfig编程方式)

    大厂面试官最常问的@Configuration+@Bean(JDKConfig编程方式)   现在大部分的Spring项目都采用了基于注解的配置,采用了@Configuration 替换标签的做法.一 ...

  7. SpringBoot 2.0 编程方式配置,不使用默认配置方式

    SpringBoot的一般配置是直接使用application.properties或者application.yml,因为SpringBoot会读取.perperties和yml文件来覆盖默认配置: ...

  8. ASP.NET MVC下的四种验证编程方式

    ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表,但是在真正执行目标Action方法之前,还需要对绑定的参数实施验证以确保其有效性,我们将针对参数的验证成为Model绑定 ...

  9. ASP.NET MVC下的四种验证编程方式【转】

    ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表,但是在真正执行目标Action方法之前,还需要对绑定的参数实施验证以确保其有效 性,我们将针对参数的验证成为Model绑 ...

随机推荐

  1. CodeForces 438D 线段树 剪枝

    D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...

  2. Laravel 之Auth用户认证

    (1)生成Auth所需文件 打开phpstorm的命令行: php artisan make:auth 生成成功后,打开web.php, 发现多了如下代码: Auth::routes(); Route ...

  3. 读取编码器信息Python2.7和Python3.3版本差异及解决,一次订阅多次调用callback的解决

    1. Python3.3以字节类型返回编码器信息,b'...',BUF: b'\xc3KOO\x00OO\x00OO\x00OO\x00OO\x00\x03\x00\x00\x00\x00\x99R\ ...

  4. 使用CSS去除 去掉超链接的下划线方法

    我们可以用CSS语法来控制超链接的形式.颜色变化,为什么链接一定要使用下划线和颜色区分呢? 其主要原因主要是考虑到   1.视力差的人 2.色盲的人 ... 下面我们做一个这样的链接:未被点击时超链接 ...

  5. Python机器学习--回归

    线性回归 # -*- coding: utf-8 -*- """ Created on Wed Aug 30 19:55:37 2017 @author: Adminis ...

  6. [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 ...

  7. hadoop优质链接

    http://wiki.apache.org/hadoop/FAQ

  8. [BLE]CC2640之ADC功能实现和供电电压的採集

    一.开篇 Write programs that do one thing and do it well ~~~~~ 发现非常多人关于使用CC2640/CC2650的过程中比較难以应对的问题就是实现A ...

  9. Android——ListView优化

    1.ListView基本概念 列表显示需要三个元素: ListView:用来展示列表的View. 适配器:用来把数据映射到ListView上 数据:具体的将被映射的字符串,图片或基本组件 适配器类型分 ...

  10. HDOJ 1217 Arbitrage(拟最短路,floyd算法)

    Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...