使用带有 XML 配置文件的 Spring 时如何调用 BeanFactoryPostProcessor.postProcessBeanFactory 方法? [英] How to call BeanFactoryPostProcessor.postProcessBeanFactory method when use Spring with XML configuration file?

查看:17
本文介绍了使用带有 XML 配置文件的 Spring 时如何调用 BeanFactoryPostProcessor.postProcessBeanFactory 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配置文件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 永远不会被调用,我错了,请帮助.

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

我在这里做了一个小测试项目https://github.com/yujiaao/spring4test

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

推荐答案

感谢 @M.Deinum 问题解决了,我将更改发布到 github.主要是我误解了M. Deinum所说的ApplicationContextBeanFactory的用法.将 DefaultListableBeanFactory 更改为 GenericApplicationContext,它现在可以工作了.

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.

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

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.

这篇关于使用带有 XML 配置文件的 Spring 时如何调用 BeanFactoryPostProcessor.postProcessBeanFactory 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆