1. 默认情况下,使用PropertyPlaceholderConfigurer多实例装配出现异常

在项目中尝试 在不同的spring的配置文件中分别引入相应的properties文件,这样会在spring配置文件中配置多个PropertyPlaceholderConfigurer实例,但是这样使用的话就会出现key找不到的问题,异常信息如下:

“ Could not resolve placeholder 'key2”

信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@38882d9a: defining beans [propertyConfigurer1,propertyConfigurer2,serviceA,serviceB,resourceServiceImpl]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'serviceA' defined in class path resource [applicationContext.xml]: Could not resolve placeholder 'key2'
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:287)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:663)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:638)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.simonme.srcstudy.spring3.demo.service.BeanAssemblyTest.main(BeanAssemblyTest.java:34)

配置形式如下(为了分析问题,配置形式做了简化,但是体现出了原本的意思):

<bean id="propertyConfigurer1"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>
classpath:conf/test/test1.properties
</value>
</list>
</property>
</bean> <bean id="propertyConfigurer2"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>
classpath:conf/test/test2.properties
</value>
</list>
</property>
</bean> <bean id="serviceA" class="org.simonme.srcstudy.spring3.demo.service.impl.ServiceAImpl">
<property name="field" value="field4AInstacne"/>
<property name="key1" value="${key1}"/>
<property name="key2" value="${key2}"/>
</bean>

Java的service代码如下:

public class ServiceAImpl implements ServiceA
{ private String field; private String key1; private String key2; /**
* 模拟返回测试数据
* @return
*/
@Override
public String queryA()
{
System.out.println("key1:" + key1);
System.out.println("key2:" + key2);
return "Query A Result" + field;
} public String getField()
{
return field;
} public void setField(String field)
{
this.field = field;
} public String getKey1()
{
return key1;
} public void setKey1(String key1)
{
this.key1 = key1;
} public String getKey2()
{
return key2;
} public void setKey2(String key2)
{
this.key2 = key2;
}
}

2. 如何装配能不出异常

如果仍然需要使用两个(或多个)PropertyPlaceholderConfigurer实例进行装配,怎样才能解决上面的异常?

<bean id="propertyConfigurer1"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>
classpath:conf/test/test1.properties
</value>
</list>
</property>
</bean>

<property name="ignoreUnresolvablePlaceholders" value="true"/>

加上上面的一行,表示可以忽略未解析到的占位符。这样就不会报错。如果对每个PropertyPlaceholderConfigurer实例都配置了这句话(忽略未解析占位符错误),那什么时候检查未解析到的占位符呢?

3. 如何解决占位符未配置的检查问题?不完美

对于未解析到的占位符,可以通过order属性来调整bean装配的优先级,然后在最后装配的PropertyPlaceholderConfigurer实例上面启用未解析到的占位符检查。

4.使PropertyPlaceholderConfigurer多实例装配出现异常对应的spring的代码在哪里

分析上述问题涉及的spring代码主要在哪里?

在spring bean装配时,一个PropertyPlaceholderConfigurer就是一个后置处理器BeanFactoryPostProcessor。在装配完PropertyPlaceholderConfigurer之后,就会触发org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(Collection<? extends BeanFactoryPostProcessor>, ConfigurableListableBeanFactory)方法,代码如下:

/**
* Invoke the given BeanFactoryPostProcessor beans.
*/
private void invokeBeanFactoryPostProcessors(
Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) { for (BeanFactoryPostProcessor postProcessor : postProcessors) {
postProcessor.postProcessBeanFactory(beanFactory);
}
}

每调用完一个BeanFactoryPostProcessor之后,就会去解析所有的bean中引用properties的占位符,这时就会出现占位符不能解析的问题(不能解析的占位在后面的BeanFactoryPostProcessor中,也就是PropertyPlaceholderConfigurer实例)。

spring的多个PropertyPlaceholderConfigurer实例装配的问题的更多相关文章

  1. Spring第一课:基于XML装配bean(四),三种实例化方式:默认构造、静态工厂、实例工厂

    Spring中基于XML中的装配bean有三种方式: 1.默认构造 2.静态工厂 3.实例工厂 1.默认构造 在我们在Spring的xml文件中直接通过:     <bean id=" ...

  2. Spring(二)__bean的装配

    Bean的装配: 在spring容器内拼凑bean叫做装配.装 配bean的时候,需要告诉容器哪些bean 以及容器如何使用依赖注入将它们配合在一起. 上下文定义文件的根元素是<beans> ...

  3. 8 -- 深入使用Spring -- 2...6 Spring 4.0 增强的自动装配和精确装配

    8.2.6 Spring 4.0 增强的自动装配和精确装配 Spring提供了@Autowired 注解来指定自动装配,@Autowired可以修饰setter方法.普通方法.实例变量和构造器等.当使 ...

  4. Spring入门1. IoC入门实例

    Spring入门1. IoC入门实例 Reference:Java EE轻量级解决方案——S2SH 前言: 之前学习过关于Spring的一点知识,曾经因为配置出现问题,而总是被迫放弃学习这些框架技术, ...

  5. #Spring实战第二章学习笔记————装配Bean

    Spring实战第二章学习笔记----装配Bean 创建应用对象之间协作关系的行为通常称为装配(wiring).这也是依赖注入(DI)的本质. Spring配置的可选方案 当描述bean如何被装配时, ...

  6. Spring 笔记(三)Bean 装配

    前言 Spring 有两大核心,也就分成两份笔记分别记录. 其一是管理应用中对象之间的协作关系,实现方式是依赖注入(DI),注入依赖的过程也被称为装配(Wiring). 基于 JavaConfig 的 ...

  7. spring 配置bean-自己主动装配

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qilixiang012/article/details/28260477 概要:(蓝色为本节所讲) ...

  8. spring bean的作用域和自动装配

    1 Bean的作用域 l  singleton单列:整个容器中只有一个对象实例,每次去访问都是访问同一个对象  默认是单列 l  prototype原型: 每次获取bean都产生一个新的对象,比如Ac ...

  9. Spring中IoC的入门实例

    Spring中IoC的入门实例 Spring的模块化是很强的,各个功能模块都是独立的,我们可以选择的使用.这一章先从Spring的IoC开始.所谓IoC就是一个用XML来定义生成对象的模式,我们看看如 ...

随机推荐

  1. one_person年轻的程序员

    回顾大学三年,通过良师的教导和自身的刻苦学习,我已初步掌握如何运用计算机编程,也养成了认真对待学习和工作的好习惯! 在思想品德上,本人有良好道德修养,并有坚定的政治方向.我热爱祖国,热爱人民,遵纪守法 ...

  2. PHP require和include的区别

    require一个文件存在错误的话,那么程序就会中断执行了,并显示致命错误 include一个文件存在错误的话,那么程序不会中端,而是继续执行,并显示一个警告错误. 以下为补充:1. include有 ...

  3. Hdu5093 Battle ships 二分图

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission( ...

  4. IIS8的证书设置

    首先,打开IIS的网站,找到“服务器证书” 然后根据需要创建证书 创建好后,如果某一个网站(注意是网站,不是应用程序集)需要使用https则, 在右侧“绑定”一项中添加新的https连接,并选择对应的 ...

  5. markdown 常用语法 (在macdown内使用正常)

    顺便附上 MacDown的官网,我觉得MacDown挺好用的,推荐给大家! #一级标题 ##二级标题 ###三级标题 ####四级标题 #####五级标题 ######六级标题 *** ###使用分割 ...

  6. 读取properties文件以及properties的用法

    package cn.util; import java.io.IOException; import java.io.InputStream; import java.util.Properties ...

  7. PHP-Redis扩展使用手册(二)

    /* 根据多个key获取多个value,不存在的key返回false getMultiple是别名? * @param array 包含key的数组 * @return array 返回key对应va ...

  8. ZeroMQ接口函数之 :zmq_ctx_set - 设置环境上下文属性

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_ctx_set zmq_ctx_set(3) ØMQ Manual - ØMQ/3.2.5 Name zmq_ct ...

  9. NGUI实现技能CD效果

    在NGUI中使用Sprite的遮罩效果可以很轻松的实现技能CD效果. 具体实现步骤: ①新建一个技能图标的Sprite 如图中的Skill001,再在该技能Sprite上添加一个Sprite做遮罩, ...

  10. antlr.collections.AST.getLine()I异常

        antlr.collections.AST.getLine()I异常 Struts+hibernate+spring项目经常遇到问题 因为Struts自带的antlr-2.7.2.jar,比H ...