- 浏览: 630389 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
hsluoyz:
现在新推出了一个权限框架,叫jCasbin(https://g ...
Shiro 权限框架使用总结 -
飕飕飞:
比如说 我用私匙加密一段数据,并加密成功。那签名时用什么生成的 ...
Java使用RSA加密解密签名及校验 -
文艺吧网:
楼主讲的好详细,这里有整套 Shiro demo http:/ ...
Shiro 权限框架使用总结 -
nanshanmu:
333引用[url][*]||||[/flash][/flas ...
SpringMVC中返回值处理 -
变脸小伙:
) 业务类在Spring配置 ...
整合Struts2与Spring以及spring的自动装配
先来看下A和B两个模块
A模块和B模块都分别拥有自己的Spring XML配置,并分别拥有自己的配置文件:
A模块
A模块的Spring配置文件如下:
Xml代码 收藏代码
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="classpath*:conf/conf_a.properties"/>
<bean class="com.xxx.aaa.Bean1"
p:driverClassName="${modulea.jdbc.driverClassName}"
p:url="${modulea.jdbc.url}"
p:username="${modulea.jdbc.username}"
p:password="${modulea.jdbc.password}"/>
</beans>
其配置文件位于类路径conf/conf_a.properties中:
Xml代码 收藏代码
modulea.jdbc.driverClassName=com.mysql.jdbc.Driver
modulea.jdbc.username=cartan
modulea.jdbc.password=superman
modulea.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8
B模块
B模块的Spring配置文件如下:
Xml代码 收藏代码
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="classpath*:conf/conf_b.properties"/>
<bean class="com.xxx.bbb.Bean1"
p:driverClassName="${moduleb.jdbc.driverClassName}"
p:url="${moduleb.jdbc.url}"
p:username="${moduleb.jdbc.username}"
p:password="${moduleb.jdbc.password}"/>
</beans>
其配置文件位于类路径conf/conf_b.properties中:
Java代码 收藏代码
moduleb.jdbc.driverClassName=com.mysql.jdbc.Driver
moduleb.jdbc.username=cartan
moduleb.jdbc.password=superman
moduleb.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8
问题来了
单独运行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:
Xml代码 收藏代码
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!--<context:property-placeholder location="classpath*:conf/conf_a.properties"/>-->
<bean class="com.xxx.aaa.Bean1"
p:driverClassName="${modulea.jdbc.driverClassName}"
p:url="${modulea.jdbc.url}"
p:username="${modulea.jdbc.username}"
p:password="${modulea.jdbc.password}"/>
</beans>
B模块b.xml:
Xml代码 收藏代码
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!--<context:property-placeholder location="classpath*:conf/conf_b.properties"/>-->
<bean class="com.xxx.bbb.Bean1"
p:driverClassName="${moduleb.jdbc.driverClassName}"
p:url="${moduleb.jdbc.url}"
p:username="${moduleb.jdbc.username}"
p:password="${moduleb.jdbc.password}"/>
</beans>
集成:
Xml代码 收藏代码
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="classpath*:conf/conf*.properties"/>
<import resource="a.xml"/>
<import resource="b.xml"/>
</beans>
进一步思考
为什么啊?Spring为什么要这样呢?细想想是有道理的,一个项目或一个系统的配置应该放在一起,不宜分散。
这样才可以做到统一管控,否则到处都有配置,到底是加载哪个配置文件呢?有时你还会不小心让JAR中的Spring配置文件加载一个位于JAR中的属性文件,而外面有更改不了。如果Spring使用了这种机制,即使JAR包中的Spring配置文件使用<context:property-placeholder/>引用到JAR中的属性文件,只要你要外而的Spring配置文件中显示提供一个<context:property-placeholder/>指定另一个属性文件 ,就可以覆盖JAR中的默认配置了。
想了一想,Spring这样做是利大于弊的。
A模块和B模块都分别拥有自己的Spring XML配置,并分别拥有自己的配置文件:
A模块
A模块的Spring配置文件如下:
Xml代码 收藏代码
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="classpath*:conf/conf_a.properties"/>
<bean class="com.xxx.aaa.Bean1"
p:driverClassName="${modulea.jdbc.driverClassName}"
p:url="${modulea.jdbc.url}"
p:username="${modulea.jdbc.username}"
p:password="${modulea.jdbc.password}"/>
</beans>
其配置文件位于类路径conf/conf_a.properties中:
Xml代码 收藏代码
modulea.jdbc.driverClassName=com.mysql.jdbc.Driver
modulea.jdbc.username=cartan
modulea.jdbc.password=superman
modulea.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8
B模块
B模块的Spring配置文件如下:
Xml代码 收藏代码
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="classpath*:conf/conf_b.properties"/>
<bean class="com.xxx.bbb.Bean1"
p:driverClassName="${moduleb.jdbc.driverClassName}"
p:url="${moduleb.jdbc.url}"
p:username="${moduleb.jdbc.username}"
p:password="${moduleb.jdbc.password}"/>
</beans>
其配置文件位于类路径conf/conf_b.properties中:
Java代码 收藏代码
moduleb.jdbc.driverClassName=com.mysql.jdbc.Driver
moduleb.jdbc.username=cartan
moduleb.jdbc.password=superman
moduleb.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8
问题来了
单独运行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:
Xml代码 收藏代码
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!--<context:property-placeholder location="classpath*:conf/conf_a.properties"/>-->
<bean class="com.xxx.aaa.Bean1"
p:driverClassName="${modulea.jdbc.driverClassName}"
p:url="${modulea.jdbc.url}"
p:username="${modulea.jdbc.username}"
p:password="${modulea.jdbc.password}"/>
</beans>
B模块b.xml:
Xml代码 收藏代码
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!--<context:property-placeholder location="classpath*:conf/conf_b.properties"/>-->
<bean class="com.xxx.bbb.Bean1"
p:driverClassName="${moduleb.jdbc.driverClassName}"
p:url="${moduleb.jdbc.url}"
p:username="${moduleb.jdbc.username}"
p:password="${moduleb.jdbc.password}"/>
</beans>
集成:
Xml代码 收藏代码
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="classpath*:conf/conf*.properties"/>
<import resource="a.xml"/>
<import resource="b.xml"/>
</beans>
进一步思考
为什么啊?Spring为什么要这样呢?细想想是有道理的,一个项目或一个系统的配置应该放在一起,不宜分散。
这样才可以做到统一管控,否则到处都有配置,到底是加载哪个配置文件呢?有时你还会不小心让JAR中的Spring配置文件加载一个位于JAR中的属性文件,而外面有更改不了。如果Spring使用了这种机制,即使JAR包中的Spring配置文件使用<context:property-placeholder/>引用到JAR中的属性文件,只要你要外而的Spring配置文件中显示提供一个<context:property-placeholder/>指定另一个属性文件 ,就可以覆盖JAR中的默认配置了。
想了一想,Spring这样做是利大于弊的。
发表评论
-
context:component-scan扫描使用上的容易忽略的use-default-filters
2015-11-13 21:28 804问题 如下方式可以成功扫描到@Controller注解的Bea ... -
MongoDB Java Driver操作指南
2015-08-04 21:42 2534MongoDB为Java提供了非常丰富的API操作,相比关系 ... -
Spring3自定义环境配置 <beans profile="">
2015-04-22 10:51 1263摘自springside3 Spring 3.1的功能 ... -
json-rpc 1.0规范解读
2015-04-17 14:21 1314JSON可能是这个地球上 ... -
request.getParameter()、request.getInputStream()和request.getReader()
2015-03-30 11:16 2996大家经常 用servlet和jsp, ... -
微信企业号上传下载多媒体文件接口详解演示-java
2015-03-27 15:37 6291企业在使用接口时,对多媒体文件、多媒体消息的获取和调用等操作 ... -
java如何得到GET和POST请求URL和参数列表
2015-03-13 16:28 3118在servlet中GET请求可以通过HttpServletR ... -
spring jms _ activemq
2015-01-05 13:50 915参考链接: http://bsnyderblog.blogsp ... -
读取配置信息
2014-12-29 18:08 843第一种方法是使用java.io和java.util包,缺点是路 ... -
spring 事件机制
2014-11-14 14:17 973在Spring中已经定义的五 ... -
spring InitializingBean接口
2014-10-27 01:33 632最近工作需要得到sping中的每个事物需要执行的sql, ... -
spring InitializingBean接口
2014-10-27 01:33 962最近工作需要得到sping中的每个事物需要执行的sql,称 ... -
四种常见的 POST 提交数据方式
2014-09-23 11:38 1618HTTP/1.1 协议规定的 HTTP 请求方法有 OPT ... -
使用 Java 配置进行 Spring bean 管理
2014-07-31 17:58 845Spring bean 是使用传统的 XML 方法配置的。在 ... -
怎么使用Servlet 3.0中的上传文件呢?
2014-07-31 15:20 937Spring 3.1开始提供了Servlet 3.0的支持。 ... -
Spring mvc 拓展使用
2014-07-31 15:08 974原文链接:http://my.oschina.net/u/11 ... -
Spring MVC handler method 参数绑定常用的注解
2014-07-31 10:36 1994参考链接:http://csjava.bl ... -
SpringMVC中使用Interceptor拦截器
2014-06-30 15:18 839SpringMVC 中的Interceptor 拦 ... -
AOP的底层实现-CGLIB动态代理和JDK动态代理
2014-05-04 16:58 1032AOP是目前Spring框架中的 ... -
Spring AOP 实现原理与 CGLIB 应用
2014-05-04 16:39 719AOP(Aspect Orient Programmi ...
相关推荐
context:property-placeholder 和util:properties 博客:https://blog.csdn.net/u010476739/article/details/76735527
主要介绍了Spring整合Mybatis使用<context>时的坑 的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
<context:property-placeholder location="classpath:jdbc.properties" /> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name...
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据库连接池 --> <bean id="dataSource" class=...
<context:property-placeholder location="classpath:jdbc.properties" /> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name=...
<context:property-placeholder location="classpath:system.properties"/> <!--服务端配置--> <bean id="rpcServer" class="com.yingjun.rpc.server.RPCServer"> <constructor-arg name=...
15. <context:property-placeholder location="classpath:/hibernate.properties" /> 16. 17. <bean id="sessionFactory" 18. class="org.springframework.orm.hibernate3.annotation....
请注意,这里面有一个方法名 selectUserByID 必须与 User.xml 里面配置的 select 的id 对应(<select id="selectUserByID") 重写测试代码 程序代码 程序代码 public static void main(String[] args) { ...
#include <afxres.h> // standard resource IDs #endif #ifndef __AFXCOLL_H__ #include <afxcoll.h> // standard collections #endif #ifdef _AFX_MINREBUILD #pragma component(minrebuild, off) #endif #...
This property is often called serializability. For example, a second transaction traversing the doubly linked list mentioned above would see the list before or after the insert, but it will see only ...
3.2.2. 高级使用:持久一个 DbTable 结果对象 3.2.3. 高级用法示例 3.3. 摘要式认证 3.3.1. 简介 3.3.2. 规范(Specifics) 3.3.3. 身份(Identity) 3.4. HTTP 认证适配器 3.4.1. 简介 3.4.2. 设计回顾 ...