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. PatentTips - GPU Support for Blending

    Graphics processing units (GPUs) are specialized hardware units used to render 2-dimensional (2-D) a ...

  2. mysqldumpslow不是内部或外部命令,也不是可运行的程序

    mysqldumpslow不是内部或外部命令,也不是可运行的程序 window系统,mysql版本:5.7.14. 使用mysqldumpslow命令分析日志时,报错:mysqldumpslow'不是 ...

  3. echarts 图表用例

    参考博客:http://blog.csdn.net/verne_feng/article/details/51731653 http://echarts.baidu.com/echarts2/doc/ ...

  4. 死磕 java同步系列之自己动手写一个锁Lock

    问题 (1)自己动手写一个锁需要哪些知识? (2)自己动手写一个锁到底有多简单? (3)自己能不能写出来一个完美的锁? 简介 本篇文章的目标一是自己动手写一个锁,这个锁的功能很简单,能进行正常的加锁. ...

  5. jquery的固定定位效果

    今天做了个固定定位的效果.比如对导航需要进行固定定位效果: 当没有滚动到导航下面,导航正常显示. 当滚动到导航下面,导航就固定到顶部. 这个效果使用了jquery的方法实现,具体思路为: 1)首先获取 ...

  6. Java 5/Java 6/Java7/Java 8新特性收集

    前言: Java 8对应的JDK版本为JDK8,而官网下载回来安装的时候,文件夹上写的是JDK1.8,同一个意思.(而这个版本命名也是有规律的,以此类推) 一.Java 5 1.https://seg ...

  7. Info.plist 的字段解释

    bundle字段 这些字段名都是XML中的名称,在xcode的属性编辑器中,名字并不相同 bundle目录中的属性列表详细描述了有关该bundle的信息.Finder和一些系统API在一些情况下会使用 ...

  8. k-svd字典学习,稀疏编码

    1.     K-SVD usage: Design/Learn a dictionary adaptively to betterfit the model and achieve sparse s ...

  9. wc递归统计代码行数

    find /path -name '*.cpp' |xargs wc -l

  10. overflow(超出部分省略号)

    溢出:overflow:visible/hidden/scroll/auto/inherit: visible:默认值.不剪切.hidden:超出部分剪切.没有滚动条.scroll:超出部分有滚动条. ...