2

I have a configuration file beans.xml:

<?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-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        ">


    <bean id="myBeanFactoryPostProcessor" class="com.ssll.MyBeanFactoryPostProcessor" />

    <bean id="myobj" class="com.ssll.MyFoo">
            <property name="realname" value="${dummy}" />
     </bean>
</beans>

Here, com.ssll.MyBeanFactoryPostProcessor is a class:

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {  

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {  
        System.out.println("This is expected to called when the BeanFactory is created");  

        Properties p  = Config.getZooProperties();
        PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
        cfg.setProperties(p);
        cfg.postProcessBeanFactory(beanFactory);     
    }  

}  

But the postProcessBeanFactory is never called, where I am wrong, please help.

I made a small test project here https://github.com/yujiaao/spring4test

Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57
  • where is application.properties. This is used when you pick value from properties file – spandey Feb 06 '18 at 10:39
  • I want to get properties from zookeeper, there is a dynamic created `Properites` object by `BeanFactoryPostProcessor` to imitation such away. – Yu Jiaao Feb 06 '18 at 10:44
  • 2
    Your test project is wrong (and overly complicated). `BeanFactoryPostProcessor`s only work in an `ApplicationContext` not in a plain `BeanFactory`. – M. Deinum Feb 06 '18 at 12:01
  • Thanks, you get me to the right way – Yu Jiaao Feb 06 '18 at 12:03

1 Answers1

0

Thanks to @M. Deinum the problem solved, I post the change to github. Mainly, I misunderstood the usage of ApplicationContext and BeanFactory as M. Deinum said. Change DefaultListableBeanFactory to GenericApplicationContext and it works now.

This post BeanFactory vs ApplicationContext and this link https://docs.spring.io/spring/docs/2.5.x/reference/beans.html#context-introduction-ctx-vs-beanfactory explain the difference in details.

I answered my question in hope that it will helpful for someone else.

Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57