我有一个配置文件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>这里,com.ssll.MyBeanFactoryPostProcessor是一个类:
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);
}
} 但是postProcessBeanFactory从来不会被调用,在我错的地方,请帮助我。
我在这里做了一个小的测试项目https://github.com/yujiaao/spring4test
发布于 2018-02-06 22:48:17
多亏了@M. Deinum问题解决了,我把这个改变发布到了github上。主要是,我误解了ApplicationContext和BeanFactory的用法,正如M. Deinum所说的那样。将DefaultListableBeanFactory更改为GenericApplicationContext,它现在就可以工作了。
这篇文章BeanFactory vs ApplicationContext和这个链接https://docs.spring.io/spring/docs/2.5.x/reference/beans.html#context-introduction-ctx-vs-beanfactory详细解释了不同之处。
我回答了我的问题,希望能对其他人有所帮助。
https://stackoverflow.com/questions/48640952
复制相似问题