Announcement Announcement Module
Collapse
No announcement yet.
Problem in the comparison and update on the class PropertyPlaceholderConfigurer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem in the comparison and update on the class PropertyPlaceholderConfigurer

    I have a dynamic file to replace the values ​​of the properties of the file applicationContext.xml
    But it does not work, because in line 140 in class BeanDefinitionVisitor he makes a comparison of the objects to see if it was amended, as it uses the same instance he has just not updating my object in context.

    Code:
    public class BeanDefinitionVisitor {
    ....
    protected void visitPropertyValues(MutablePropertyValues pvs) {
        PropertyValue[] pvArray = pvs.getPropertyValues();
        for (PropertyValue pv : pvArray) {
           // return the same value... BUG
           Object newVal = resolveValue(pv.getValue());
           if (!ObjectUtils.nullSafeEquals(newVal, pv.getValue())) {
                pvs.add(pv.getName(), newVal);
           }
        }
    }
    ....
    }

    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"
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
        default-lazy-init="true">
        
        <util:properties id="hibernateProperties">
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="show_sql">false</prop>
            <prop key="format_sql">false</prop>
            <prop key="hbm2ddl.auto">update</prop>
        </util:properties>
    
        <context:annotation-config />
    
        <context:component-scan base-package="com.test"/>
    
    </beans>
    Code:
    import java.io.ByteArrayOutputStream;
    import java.util.Map.Entry;
    import java.util.Properties;
    
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.core.io.ByteArrayResource;
    
    public class RunSpring {
    
    	public static Properties testProp = new Properties();
    	
    	static{
    		testProp.put("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");
    	}
    		
    	/**
    	 * 
    	 * @throws Exception
    	 */
    	public void testXmlConfig() throws Exception {
    		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    		// informa que pode desligar o contexto caso estiver ocioso
    		context.registerShutdownHook();
    		// registra o arquivo de propriedades
    		registerPropertyPlaceholder(context.getBeanFactory());
    		// registra a configuração
    		context.refresh();
    		
    		Properties prop = (Properties) context.getBean("hibernateProperties");
    		
    		System.out.println(prop.get("hibernate.dialect"));
    	}
    	
    	/**
    	 * 
    	 * @param beanFactory
    	 * @throws Exception
    	 */
    	public void registerPropertyPlaceholder( ConfigurableListableBeanFactory beanFactory) throws Exception {
    		// bring in some property values from a Properties file
    		PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
    		cfg.setLocation(new ByteArrayResource(getFileProperties()));
    
    		// now actually do the replacement
    		cfg.postProcessBeanFactory(beanFactory);
    	}
    	
    	/**
    	 * 
    	 * @return
    	 * @throws Exception
    	 */
    	public static byte[] getFileProperties() throws Exception {
    		ByteArrayOutputStream bos = new ByteArrayOutputStream();
    		for(Entry<Object, Object> entry : testProp.entrySet()){
    			bos.write(String.format("%s=%s \n", entry.getKey(), entry.getValue()).getBytes("utf-8"));
    		}
    		return bos.toByteArray();
    		
    	}
    	
    	/**
    	 * @param args
    	 * @throws Exception 
    	 */
    	public static void main(String[] args) throws Exception {
    		RunSpring run = new RunSpring();
    		run.testXmlConfig();
    	}
    	
    }
    Attachment

    any idea?
    Attached Files
    Last edited by dehw; Jun 16th, 2011, 07:03 AM.

  • #2
    BeanDefinitionVisitor

    I tried to re-implement the methods visitMap, visitSet others that do not create a new instance in the class BeanDefinitionVisitor. So it updates the values ​​in beanDefinition settings, but when I do context.getBean ("blablabla") it does not search the updated data from my beanDefinition.

    Comment

    Working...
    X