Announcement Announcement Module
Collapse
No announcement yet.
Override existing properties Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Override existing properties

    Hi,

    Assuming I have a spring config xml loaded (I can't modify this xml for some reason like it's in some predefined jars).
    in this xml:

    <contextroperty-placeholder
    location="classpath:config/system.properties" />


    in system.properties, there're a lot of properties, 1 of them looks like:

    AAA.BBB.CCC = true


    I would like to override this property in runtime, how?

    I'm trying to add another config in my aggregate spring config xml:

    <contextroperty-placeholder
    location="classpath:config/system_override.properties" />

    and I have only 1 entry in system_override.properties: AAA.BBB.CCC=false

    but it seems not work.


    any solutions?


    Thanks
    Ma Ling

  • #2
    hello ,I got it :
    1.bean class
    Code:
    class Person{
    	String name;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    }
    2.test code:
    Code:
    		String contentConfig = "applicationContext-TestPlaceHolder.xml";
    		ApplicationContext context = new ClassPathXmlApplicationContext(contentConfig);
    		org.william.spring.beans.demo.Person config = (org.william.spring.beans.demo.Person) context.getBean("testPlaceHolder");
    		System.out.println(config.getName());
    3.applicationcontext xml :
    applicationContext-TestPlaceHolder2.xml:
    Code:
    <?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"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    				http://www.springframework.org/schema/beans/spring-beans.xsd
    				http://www.springframework.org/schema/context
    				http://www.springframework.org/schema/context/spring-context-3.1.xsd"
    >
    		<context:property-placeholder location="classpath:TestPlaceHolder1.properties"/>
    </beans>
    applicationContext-TestPlaceHolder.xml
    Code:
    <?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"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    				http://www.springframework.org/schema/beans/spring-beans.xsd
    				http://www.springframework.org/schema/context
    				http://www.springframework.org/schema/context/spring-context-3.1.xsd"
    				>
    		<context:property-placeholder location="classpath:TestPlaceHolder2.properties"/>  
    		<import resource="applicationContext-TestPlaceHolder2.xml"/>
    
    	<bean id="testPlaceHolder" class="org.william.spring.beans.demo.Person" >
    					<property name="name">
    						<value>${test.placeholder.name}</value>
    					</property>
    	</bean>
    </beans>
    4 properties files:
    TestPlaceHolder1.properties:
    Code:
    test.placeholder.name=test-one
    TestPlaceHolder2.properties:
    Code:
    test.placeholder.name=test-two

    Comment


    • #3
      the point is the sequence, if we want use TestPlaceHolder2.properties to override TestPlaceHolder1.properties,you should place it
      prior TestPlaceHolder1.properties.

      Comment


      • #4
        @Shenzhenguy
        It works because a property place holder configurer can not override an existing value.
        But did you try a property override configurer ?
        http://static.springsource.org/sprin...rideconfigurer
        I think this way you don't have to care about the declaration order in your xml file.

        Comment


        • #5
          @abousta
          PropertyOverrideConfigurer and PropertyPlaceholderConfigurer are BeanFactoryPostProcessors.
          BeanFactoryPostProcessors to read the configuration metadata and potentially change it before the container instantiates any beans other than BeanFactoryPostProcessors.
          That means you cann't use a BeanFactoryPostProcessor to change other BeanFactoryPostProcessors' configuration metadata.
          so,cann't use PropertyOverrideConfigurer to override PropertyPlaceholderConfigurer's properties.

          by the way, the string value of the "name" property "${test.placeholder.name}" was replaced with "test-two" by the first PropertyPlaceholderConfigurer ,then the second PropertyPlaceholderConfigurer process the value "test-two" not "${test.placeholder.name}", so the second cann't replace it to "test-one"

          Comment

          Working...
          X