您的位置:首页 > 编程语言 > Java开发

spring3.1.2 容器在实例化属性解析器问题(相同ID覆盖)

2015-08-13 10:35 639 查看
看下项目配置

<!--为项目自己身的一个属性解析器-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:path.properties</value>
<value>classpath:solr.properties</value>
</list>
</property>
</bean>
<!-- 要实例的类-->
<bean id="resourceConfig" class="com.jyt.ResourceConfig">
<property name="rootPath">
<value>${project.root.path}</value>
</property>
<property name="htmlRelativePath">
<value>html/</value>
</property>
<property name="pagorRelativePath">
<value>pagor/</value>
</property>
<property name="jytRootPath">
<value>${jyt.root}</value>
</property>
</bean>

<!-- 引入第三方lib包中的xml-->
<import resource="classpath:META-INF/applicationContext-solr.xml"/>

<!-- application_sorl.xml-->
<!-- 均衡加载sorl url -->
<bean id="lbClient" class="org.apache.solr.client.solrj.impl.LBHttpSolrClient">
<constructor-arg name="solrServerUrls" value="${lbClient.url}"/>
</bean>

<!-- 集群模式下sorl实例 -->
<bean id="cloudSolrClient" class="org.apache.solr.client.solrj.impl.CloudSolrClient">
<constructor-arg name="zkHost" value="${cloudSolrClient.url}"/>
<constructor-arg name="lbClient" ref="lbClient" />
</bean>
<!-- 此处的propertyConfigurer-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>


项目问题: 在注入resourceConfig的时候一直不能为 “rootPath赋上path.properties中的值而是复上了project.root.path字符原因:propertyConfigurer在读取配置文件时没有project.root.path属性所以将{project.root.path} 字符 原因:propertyConfigurer在读取配置文件时没有project.root.path属性所以将{project.root.path}当成文本赋给resourceConfig 项目明明读取了path.properties 为什么会没有project.root.path属性那 因为applicationContext-solr.xml中也配置了个propertyConfigurer实例 spring 在初始化容器的时候会按照在配置文件出现的顺序来初始化容器 而applicationContext-solr.xml在文件后面所以spring用applicationContext-solr.xml中的propertyConfigurer覆盖了当前文件中propertyConfigurer 而applicationContext-solr.xml中的propertyConfigurer只读取了jdbc.properties所以导致出现了此问题。

解决方法: 1.只保留一个propertyConfigurer 2.将想用的propertyConfigurer放到配置文件中的最后让spring用他

注意:项目中如果存在依赖和jar的方式 一定要注意此问题 classpath包括jar 依赖 当前项目的classes 等
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: