spring 多资源文件
来源:互联网 发布:ubuntu win7双系统安装 编辑:程序博客网 时间:2024/05/29 15:37
2.Spring中PropertyPlaceholderConfigurer多种配置方式
2.1配置单个Properties文件
<bean id="propertyConfigurerForAnalysis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:/spring/include/dbQuery.properties</value>
</property>
</bean>
<property name="location">
<value>classpath:/spring/include/dbQuery.properties</value>
</property>
</bean>
其中classpath是引用src目录下的文件写法。
2.2 当存在多个Properties文件时,配置就需使用locations了:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:/spring/include/jdbc-parms.properties</value> <value>classpath:/spring/include/base-config.properties</value> </list> </property></bean>
2.3、接下来我们要使用多个PropertyPlaceholderConfigurer来分散配置,达到整合多工程下的多个分散的Properties 文件,其配置如下:
<bean id="propertyConfigurerForProject1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="location">
<value>classpath:/spring/include/dbQuery.properties</value>
</property>
</bean>
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="location">
<value>classpath:/spring/include/dbQuery.properties</value>
</property>
</bean>
<bean id="propertyConfigurerForProject2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:/spring/include/jdbc-parms.properties</value>
<value>classpath:/spring/include/base-config.properties</value>
</list>
</property>
</bean>
<property name="order" value="2" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:/spring/include/jdbc-parms.properties</value>
<value>classpath:/spring/include/base-config.properties</value>
</list>
</property>
</bean>
其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的 Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true
关于<context:property-placeholder>的一个有趣现象
Spring
先来看下A和B两个模块
A模块和B模块都分别拥有自己的Spring XML配置,并分别拥有自己的配置文件:
A模块
A模块的Spring配置文件如下:
其配置文件位于类路径conf/conf_a.properties中:
B模块
B模块的Spring配置文件如下:
其配置文件位于类路径conf/conf_b.properties中:
问题来了
单独运行A模块,或单独运行B模块都是正常的,但将A和B两个模块集成后运行,Spring容器就启动不了了:
到底出了啥问题
随便搜索了一下,还发现很多人遇到这个问题,这个就是来自stackoverflow的问题:
http://stackoverflow.com/questions/7940452/spring-application-context-not-able-to-load-property-placeholder-properties
可惜啊,好像都没有人给出正确的解决。
那究竟是什么问题呢?也想了很久哦....终于回想起来了(写书时读过Spring源码),原来是Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了)。
而<context:property-placeholder/>这个基于命名空间的配置,其实内部就是创建一个PropertyPlaceholderConfigurer Bean而已。换句话说,即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其余的会被Spring忽略掉(其实Spring如果提供一个警告就好了)。
拿上来的例子来说,如果A和B模块是单独运行的,由于Spring容器都只有一个PropertyPlaceholderConfigurer,因此属性文件会被正常加载并替换掉。如果A和B两模块集成后运行,Spring容器中就有两个PropertyPlaceholderConfigurer Bean了,这时就看谁先谁后了, 先的保留,后的忽略!因此,只加载到了一个属性文件,因而造成无法正确进行属性替换的问题。
咋解决呢?
定位问题需要9999元钱,解决问题只需要1元钱 。
属性文件加载在统一的地方做,不要分模块加载即可。
A模块a.xml:
B模块b.xml:
集成:
进一步思考
为什么啊?Spring为什么要这样呢?细想想是有道理的,一个项目或一个系统的配置应该放在一起,不宜分散。
这样才可以做到统一管控,否则到处都有配置,到底是加载哪个配置文件呢?有时你还会不小心让JAR中的Spring配置文件加载一个位于JAR中的属性文件,而外面有更改不了。如果Spring使用了这种机制,即使JAR包中的Spring配置文件使用<context:property-placeholder/>引用到JAR中的属性文件,只要你要外而的Spring配置文件中显示提供一个<context:property-placeholder/>指定另一个属性文件 ,就可以覆盖JAR中的默认配置了。
想了一想,Spring这样做是利大于弊的。
A模块和B模块都分别拥有自己的Spring XML配置,并分别拥有自己的配置文件:
A模块
A模块的Spring配置文件如下:
其配置文件位于类路径conf/conf_a.properties中:
B模块
B模块的Spring配置文件如下:
其配置文件位于类路径conf/conf_b.properties中:
问题来了
单独运行A模块,或单独运行B模块都是正常的,但将A和B两个模块集成后运行,Spring容器就启动不了了:
引用
Could not resolve placeholder 'moduleb.jdbc.driverClassName' in string value "${moduleb.jdbc.driverClassName}"
到底出了啥问题
随便搜索了一下,还发现很多人遇到这个问题,这个就是来自stackoverflow的问题:
http://stackoverflow.com/questions/7940452/spring-application-context-not-able-to-load-property-placeholder-properties
可惜啊,好像都没有人给出正确的解决。
那究竟是什么问题呢?也想了很久哦....终于回想起来了(写书时读过Spring源码),原来是Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了)。
而<context:property-placeholder/>这个基于命名空间的配置,其实内部就是创建一个PropertyPlaceholderConfigurer Bean而已。换句话说,即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其余的会被Spring忽略掉(其实Spring如果提供一个警告就好了)。
拿上来的例子来说,如果A和B模块是单独运行的,由于Spring容器都只有一个PropertyPlaceholderConfigurer,因此属性文件会被正常加载并替换掉。如果A和B两模块集成后运行,Spring容器中就有两个PropertyPlaceholderConfigurer Bean了,这时就看谁先谁后了, 先的保留,后的忽略!因此,只加载到了一个属性文件,因而造成无法正确进行属性替换的问题。
咋解决呢?
定位问题需要9999元钱,解决问题只需要1元钱 。
属性文件加载在统一的地方做,不要分模块加载即可。
A模块a.xml:
B模块b.xml:
集成:
进一步思考
为什么啊?Spring为什么要这样呢?细想想是有道理的,一个项目或一个系统的配置应该放在一起,不宜分散。
这样才可以做到统一管控,否则到处都有配置,到底是加载哪个配置文件呢?有时你还会不小心让JAR中的Spring配置文件加载一个位于JAR中的属性文件,而外面有更改不了。如果Spring使用了这种机制,即使JAR包中的Spring配置文件使用<context:property-placeholder/>引用到JAR中的属性文件,只要你要外而的Spring配置文件中显示提供一个<context:property-placeholder/>指定另一个属性文件 ,就可以覆盖JAR中的默认配置了。
想了一想,Spring这样做是利大于弊的。
阅读全文
0 0
- spring 多资源文件
- spring加载资源文件
- spring加载资源文件
- spring 读取资源文件
- spring 读取资源文件
- Spring文件资源操作
- SPRING 加载资源文件
- spring读取资源文件
- Spring获取资源文件
- spring 加载资源文件
- 使用Spring访问资源文件
- 使用Spring访问资源文件
- Spring 中加载资源文件
- Spring 中加载资源文件
- spring直接读取资源文件
- Spring 中加载资源文件
- Spring集成多个资源文件加载问题
- Spring入门14 - 资源文件的取得
- Eclipse下Tomcat启动超时报错
- 详解蔬菜自动售货机的结构设计原理
- java学习路线图
- 项目1:分支结构程序设计体验
- Oracle-统计数量的Function
- spring 多资源文件
- 小白学习Hibernate遇到的错误以及解决办法(GenerationTarget encountered exception accepting command : Error executing)
- Implementing a CNN for Human Activity Recognition in Tensorflow
- 改进排序算法:归并排序
- 用链表+函数指针+定时器中断实现的一个软件定时器(试用于所有单片机)
- oKhttp第三方登录
- 0基础lua学习(十六)lua的多态 base
- Vim 命令行快捷键
- 对list里的数据进行大小的排序