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. POJ 3468 线段树 成段更新 懒惰标记

    A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Descr ...

  2. msp430项目编程54

    msp430综合项目---扩展项目四54 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结

  3. formSubmit

    精简代码: <form name='form0001' method="post"> .... <li id="view"><a ...

  4. 关于MySQL的事务处理及隔离级别

    原文地址 :http://blog.sina.com.cn/s/blog_4c197d420101awhc.html 事务是DBMS得执行单位.它由有限得数据库操作序列组成得.但不是任意得数据库操作序 ...

  5. Codeforces 515E Drazil and Park (ST表)

    题目链接 Drazil and Park 中文题面 传送门 如果他选择了x和y,那么他消耗的能量为dx + dx + 1 + ... + dy - 1 + 2 * (hx + hy). 把这个式子写成 ...

  6. 洛谷 P1503鬼子进村

    题目背景 小卡正在新家的客厅中看电视.电视里正在播放放了千八百次依旧重播的<亮剑>,剧中李云龙带领的独立团在一个县城遇到了一个鬼子小队,于是独立团与鬼子展开游击战. 题目描述 描述 县城里 ...

  7. hadoop2.7.1 nutch2.3 二次开发windows环境

     Hadoop windows编译: 能够略过这一段,直接下载hadoo2.7.1 bin文件.我的资源里有终于生成的winutils.exe和一些native code,放在bin文件夹即可了 參 ...

  8. CCPhysicsSprite

    #ifndef __PHYSICSNODES_CCPHYSICSSPRITE_H__ #define __PHYSICSNODES_CCPHYSICSSPRITE_H__ #include " ...

  9. 九度OJ #1437 To Fill or Not to Fil

    题目描写叙述: With highways available, driving a car from Hangzhou to any other city is easy. But since th ...

  10. Git的使用 -- 用git玩翻github,结尾有惊喜!有惊喜!有惊喜!林妙妙看了说:牛呲呼啦带闪电 (三)(超详解)

    简介 上一篇主要讲解的是Git安装及配置,这一篇就详细的从无到有的来用Git玩翻github. 一.什么是Github Github是全球最大的社交编程及代码托管网站(https://github.c ...