首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在配置文件中使用Spring调用BeanFactoryPostProcessor.postProcessBeanFactory方法?

如何在配置文件中使用Spring调用BeanFactoryPostProcessor.postProcessBeanFactory方法?
EN

Stack Overflow用户
提问于 2018-02-06 18:37:37
回答 1查看 956关注 0票数 2

我有一个配置文件beans.xml

代码语言:javascript
运行
复制
<?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是一个类:

代码语言:javascript
运行
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-06 22:48:17

多亏了@M. Deinum问题解决了,我把这个改变发布到了github上。主要是,我误解了ApplicationContextBeanFactory的用法,正如M. Deinum所说的那样。将DefaultListableBeanFactory更改为GenericApplicationContext,它现在就可以工作了。

这篇文章BeanFactory vs ApplicationContext和这个链接https://docs.spring.io/spring/docs/2.5.x/reference/beans.html#context-introduction-ctx-vs-beanfactory详细解释了不同之处。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48640952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档