扩展spring类实现自动读取配置文件
2014-04-15 11:25
751 查看
在使用spring的时候,我们使用Properties配置器把properties文件装载到Spring的上下文中,如下:
<!-- Loads a Properties instance -->
<util:properties id="evn" location="classpath:config/jndi.properties"></util:properties>
<context:property-placeholder location="classpath:config/ejb.properties" />
这样在Spring的配置文件中可以用表达式来获得load进来的properties内容,如下:
<jee:jndi-lookup jndi-name="${ejb-name}" id="login-loginBean"
lookup-on-startup="true" cache="true" proxy-interface="${ejb-interface}"
environment-ref="evn">
</jee:jndi-lookup>
既然spring已经能把properties读到它的上下文,在配置文件中轻松地使用,那么是不是也可以想办法让我们在代码中也能取到这些值?
那我们何不利用这一点,让我们能从代码中也使用这些值。
整理一下思路:
1、准备好配置文件,将properties的值设置到Bean中
2、初始化Spring上下文,加载properties文件
3、可以通过相应的bean去访问需要的properties的值
第1点很简单,不多说
第2点初始化Spring上下文,有很多方式,大家可以根据自己的方式选择其一即可。
方式1:
FileSystemXmlApplicationContext——从指定的目录中加载
ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
方式2:
ClassPathXmlApplicationContext——从classpath路径加载:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
方式3:
ServletContext servletContext = servlet.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
方式4:http://blog.csdn.net/dqatsh/article/details/3469278
第3步,查资料知道Spring使用
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 读取properties文件。
我们可以扩展这个类来满足我们的需求。 怎么办?继承!
那么我们如何在Spring中配置呢?
<bean id="configBean" class="common.tool.config.PropertyConfigure">
<!-- <property name="location" value="classpath:url.properties" /> -->
<property name="locations">
<list>
<value>url.properties</value>
<value>wsdl.properties</value>
</list>
</property>
</bean>
怎么在代码中获取properties的值,不用我多说了吧。
这样做最直接的好处是,不用我们再手写代码读取配置文件了,而且设计看起来更优雅。
<!-- Loads a Properties instance -->
<util:properties id="evn" location="classpath:config/jndi.properties"></util:properties>
<context:property-placeholder location="classpath:config/ejb.properties" />
这样在Spring的配置文件中可以用表达式来获得load进来的properties内容,如下:
<jee:jndi-lookup jndi-name="${ejb-name}" id="login-loginBean"
lookup-on-startup="true" cache="true" proxy-interface="${ejb-interface}"
environment-ref="evn">
</jee:jndi-lookup>
既然spring已经能把properties读到它的上下文,在配置文件中轻松地使用,那么是不是也可以想办法让我们在代码中也能取到这些值?
那我们何不利用这一点,让我们能从代码中也使用这些值。
整理一下思路:
1、准备好配置文件,将properties的值设置到Bean中
2、初始化Spring上下文,加载properties文件
3、可以通过相应的bean去访问需要的properties的值
第1点很简单,不多说
第2点初始化Spring上下文,有很多方式,大家可以根据自己的方式选择其一即可。
方式1:
FileSystemXmlApplicationContext——从指定的目录中加载
ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
方式2:
ClassPathXmlApplicationContext——从classpath路径加载:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
方式3:
ServletContext servletContext = servlet.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
方式4:http://blog.csdn.net/dqatsh/article/details/3469278
第3步,查资料知道Spring使用
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 读取properties文件。
我们可以扩展这个类来满足我们的需求。 怎么办?继承!
/** * @author : jl * @group : tgb8 * @Date : 2014-4-4 下午4:38:19 * @Comments : 方便开发人员使用可以使用PropertyConfigurer.getContextProperty()来取得上下文中的properties的值了。 * @Version : 1.0.0 */ public class PropertyConfigure extends PropertyPlaceholderConfigurer { private static Map<String, Object> ctxPropertiesMap; @Override protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException { super.processProperties(beanFactory, props); ctxPropertiesMap = new HashMap<String, Object>(); for (Object key : props.keySet()) { String keyStr = key.toString(); String value = props.getProperty(keyStr); ctxPropertiesMap.put(keyStr, value); } } public static Object getContextProperty(String name) { //初始化全局配置 initApplicationContext("context-properties.xml"); return ctxPropertiesMap.get(name); } private static void initApplicationContext(String key) { CacheHandler cacheHandler = CacheHandler.getInstance(); Cache cache = cacheHandler.getCacheByName("ApplicationContext"); //如果没有初始化ApplicationContext,则初始化,并放入缓存,防止下次再初始化 if (cache == null) { System.out.println("创建ApplicationContext缓存"); cache = cacheHandler.addCache("ApplicationContext"); System.out.println("初始化ApplicationContext,key="+key); ApplicationContext context = new ClassPathXmlApplicationContext( key); Element elementVal = new Element(key, context); cache.put(elementVal); } } }
那么我们如何在Spring中配置呢?
<bean id="configBean" class="common.tool.config.PropertyConfigure">
<!-- <property name="location" value="classpath:url.properties" /> -->
<property name="locations">
<list>
<value>url.properties</value>
<value>wsdl.properties</value>
</list>
</property>
</bean>
怎么在代码中获取properties的值,不用我多说了吧。
这样做最直接的好处是,不用我们再手写代码读取配置文件了,而且设计看起来更优雅。
相关文章推荐
- 扩展spring注解,读取配置文件
- java用spring实现配置文件properties的读取
- Spring依赖注入的XML配置文件的实现思想(1)——准备工作:如何读取XML的文件的内容
- dom4j实现模拟Spring读取配置文件 注入
- Spring读取xml配置文件的原理与实现
- spring-实现配置文件读取
- 读取Spring配置文件 main方法里面测试实现功能
- spring类实现自动读取配置文件
- Spring读取xml配置文件的原理与实现
- 扩展:Spring的PropertyPlaceholderConfigurer应用(自动获取配置文件的数据)
- Spring读取xml配置文件的原理与实现
- 可实现自动重载配置文件读取类
- Spring boot读取配置文件
- Spring读取配置文件
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- Spring 源码分析:Bean 加载流程概览及配置文件读取
- Spring-AOP 通过配置文件实现 后置增强
- spring mvc 在Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别
- 使用Spring 3的@value简化配置文件的读取