将Spring与XML配置文件一起使用时如何调用BeanFactoryPostProcessor.postProcessBeanFactory方法?

Yu Jiaao

我有一个配置文件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

Yu Jiaao

感谢@M。Deinum解决了问题,我将更改发布到github。我主要是误解了Deinum先生所说ApplicationContext的用法BeanFactory更改DefaultListableBeanFactoryGenericApplicationContext,现在可以使用。

这篇BeanFactory vs ApplicationContext帖子以及https://docs.spring.io/spring/docs/2.5.x/reference/beans.html#context-introduction-ctx-vs-beanfactory链接详细说明了这些区别。

我回答了我的问题,希望对其他人有所帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章