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. 【WEB基础】HTML & CSS 基础入门(5)边框与背景

    前面(HTML图片) 漂亮的网页肯定少不了边框与背景的修饰,本篇笔记就是说明如何为网页上的元素设置边框或者背景(背景颜色和背景图片). 之前,先了解一下HTML中的图片元素,因为图片标签的使用非常简单 ...

  2. linux下查看cpu使用情况

    1.在终端输入top 2.ubuntu系统自带有system monitor 3.sudo apt-get install sysstat 然后在终端输入:mpstat

  3. 洛谷 P2613 【模板】有理数取余

    P2613 [模板]有理数取余 题目描述 给出一个有理数c=\frac{a}{b}c=ba​,求c\ \bmod 19260817c mod19260817的值. 输入输出格式 输入格式: 一共两行. ...

  4. __new__ 和 __init__

    new 在新式类中负责真正的实例化对象,而__init__只是负责初始化 __new__创建的对象.一般来说 new 创建一个内存对象,也就是实例化的对象的实体,交给__init__进行进一步加工.官 ...

  5. 【sourcetree】sourcetree连接远程仓库需要登陆但是一直登陆不上的问题 解决方法

    授权类型选用 基础 .只需要登陆你在bitbucket的用户名和密码 如下 .即可成功连接远程仓库

  6. 通过Python实现自动填写调查问卷

    0X00 前言 快开学了,看到空间里面各种求填写调查问卷的,我才想起来貌似我也还没做.对于这种无意义的问卷,我是不怎么感冒的,所以我打算使用”特技”来完成,也就是python,顺便重新复习一下pyth ...

  7. Override和Overload差别,Overloaded的方法能否够改变返回值的类型?

    Overload是重载的意思, Override是覆盖的意思,也就是重写. 重载Overload表示同一个类中能够有多个名称同样的方法,但这些方法的參数列表各不同样(即參数个数或类型不同). 重写Ov ...

  8. [Javascript] Use a custom sort function on an Array in Javascript

    Sorting in Javascript with sort uses lexical sorting by default, which means it will sort in alphabe ...

  9. SAP 锁对象 基本概念与基本操作 SE11

      一.SAP为什么要设置锁:     1,保持数据的一致性     假设几个用户要訪问相同的资源,须要找到一种同步訪问的方法去保持数据的一致性.比方说,在航班预订系统中,须要检查还有没有空座位,当检 ...

  10. POJ - 1062 昂贵的聘礼(最短路Dijkstra)

    昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u SubmitStatus Descr ...