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. PV操作

    学习PV操作之前,我们首先来了解两个很基础的概念: 同步.互斥: 同步:其实说同步还不如说”协作“,就是我们的目标只有一个,我们奔着同一个目标去的,都是在大家的努力下共同完成这么一件事情.还是比较容易 ...

  2. 数字转IP地址函数

    --------------------------------------------------------------------- -- Author : htl258(Tony) -- Da ...

  3. 版本管理之Git(二):Win7上Git安装及简单配置过程

    一.安装包 msysgit(Windows版本的Git) 下载地址:http://code.google.com/p/msysgit/downloads/list?q=full+installer+o ...

  4. Linux CentOS6.5下安装Oracle ASM

    Oracle版本:Oracle 11g 1.确定自己的Linux版本: [root@localhost ~]#uname -r 2.6.32-431.el6.x86_64 2.6.32-431.el6 ...

  5. mysql在线修改表结构大数据表的风险与解决办法归纳

    整理这篇文章的缘由: 互联网应用会频繁加功能,修改需求.那么表结构也会经常修改,加字段,加索引.在线直接在生产环境的表中修改表结构,对用户使用网站是有影响. 以前我一直为这个问题头痛.当然那个时候不需 ...

  6. poco json 中文字符,抛异常JSON Exception -->iconv 转换 备忘录。

    起因 最近linux服务器通信需要用到json. jsoncpp比较出名,但poco 1.5版本以后已经带有json库,所以决定使用poco::json(linux 上已经用到了poco这一套框架). ...

  7. 使用Git进行项目管理

    首先在https://git.oschina.net进行注册以及登陆 登陆进去之后,如果想要创建项目,可以在 点击加号按钮,进行项目创建 3.这里以创建私有项目为例: 输入完成后,点击“创建”,进入下 ...

  8. UVa #11582 Colossal Fibonacci Numbers!

    巨大的斐波那契数 The i'th Fibonacci number f (i) is recursively defined in the following way: f (0) = 0 and  ...

  9. Linux中syntax error near unexpected token 错误提示解决方法

    Linux中syntax error near unexpected token ... 错误提示有一般有两种原因: 1)window和Linux下换行符不一致导致 window下的换行和Linux下 ...

  10. Entity Framework 基于方法的查询语法

      实体框架(Entity Framework )是 ADO.NET 中的一套支持开发面向数据的软件应用程序的技术. LINQ to Entities 提供语言集成查询 (LINQ) 支持,它允许开发 ...