Problems with multiple Propertyplaceholderconfigurer instances assembly in spring

Source: Internet
Author: User

1. By default, multiple instance assembly with Propertyplaceholderconfigurer exception

In the project, try to introduce the respective properties file in different spring configuration files, which will configure multiple Propertyplaceholderconfigurer instances in the spring configuration file. However, the use of the words will appear key can not find the problem, the exception information is as follows:

"Could not resolve placeholder ' Key2"

information: Destroying singletons in org.s[email protected]38882d9a:defining beans [PropertyConfigurer1, Propertyconfigurer2,servicea,serviceb,resourceserviceimpl]; Root of factory hierarchyexception in thread"Main" org.springframework.beans.factory.BeanDefinitionStoreException:Invalid bean definition with name ' ServiceA ' Defined inclassPath 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:36V) at Org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors ( Abstractapplicationcontext.java:33]) 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)

The configuration form is as follows (in order to analyze the problem, the configuration is simplified, but the original meaning is shown):

<BeanID= "PropertyConfigurer1"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    < Propertyname= "Locations">        <List>            <value>classpath:conf/test/test1.properties</value>        </List>    </ Property></Bean><BeanID= "PropertyConfigurer2"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    < Propertyname= "Locations">        <List>            <value>classpath:conf/test/test2.properties</value>        </List>    </ Property></Bean><BeanID= "ServiceA"class= "Org.simonme.srcstudy.spring3.demo.service.impl.ServiceAImpl">    < Propertyname= "Field"value= "Field4ainstacne"/>    < Propertyname= "Key1"value= "${key1}"/>    < Propertyname= "Key2"value= "${key2}"/></Bean>

The service code for Java is as follows:

 Public classServiceaimplImplementsservicea{PrivateString field; PrivateString Key1; PrivateString Key2; /*** Simulated return test data *@return     */@Override PublicString Querya () {System.out.println ("Key1:" +key1); System.out.println ("Key2:" +Key2); return"Query A Result" +field; }     PublicString GetField () {returnfield; }     Public voidSetField (String field) { This. field =field; }     PublicString getKey1 () {returnKey1; }     Public voidsetKey1 (String key1) { This. Key1 =Key1; }     PublicString GetKey2 () {returnKey2; }     Public voidSetKey2 (String key2) { This. Key2 =Key2; }}

2. How to assemble can not be abnormal

If you still need to assemble with two (or more) Propertyplaceholderconfigurer instances, how can I resolve the above exception?

<BeanID= "PropertyConfigurer1"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    < Propertyname= "Ignoreunresolvableplaceholders"value= "true"/>    < Propertyname= "Locations">        <List>            <value>classpath:conf/test/test1.properties</value>        </List>    </ Property></Bean>

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

The above line indicates that the unresolved placeholders can be ignored. This will not be an error. If you configure this sentence for each propertyplaceholderconfigurer instance (ignoring unresolved placeholder errors), when do you check for unresolved placeholders?

3. How do I troubleshoot a placeholder for a check problem that is not configured? Not perfect

For non-resolved placeholders, you can adjust the priority of the Bean assembly with the Order property, and then enable unresolved placeholder checks on the last assembled Propertyplaceholderconfigurer instance.

4. Make Propertyplaceholderconfigurer multi-instance assembly where is the spring code corresponding to the exception

What is the main spring code involved in analyzing the above issues?

When the spring Bean is assembled, a propertyplaceholderconfigurer is a post processor beanfactorypostprocessor. After assembling the Propertyplaceholderconfigurer, Will trigger Org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors (Collection <? Extends Beanfactorypostprocessor>, configurablelistablebeanfactory) method, the code is as follows:

/***/privatevoid  invokebeanfactorypostprocessors (        Collection  extends beanfactorypostprocessor> postprocessors, Configurablelistablebeanfactory beanfactory ) {    for  (beanfactorypostprocessor postprocessor:postprocessors) {        Postprocessor.postprocessbeanfactory (beanfactory);    }}

After each invocation of a beanfactorypostprocessor, the placeholder for the properties in all beans is parsed, and the placeholder cannot be resolved ( The placeholder that cannot be resolved is in the back of the Beanfactorypostprocessor, which is the Propertyplaceholderconfigurer instance.

Problems assembling multiple propertyplaceholderconfigurer instances of spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.