【问题标题】:Spring - BeanPostProcessor interfaceSpring - BeanPostProcessor 接口
【发布时间】:2013-10-07 16:00:29
【问题描述】:

我刚开始学习春天。在实现一个示例程序时,我遇到了一个问题,并想在这个论坛上查看以获取答案。

下面是spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org    /dtd/spring-beans-2.0.dtd">

<beans>
    <bean id="triangle" class="org.springdemo.Triangle" autowire="byName">
    </bean>

    <bean id="employee" class="org.springdemo.emp.Employee" autowire="byName">
    
    </bean>

    <bean id="address" class="org.springdemo.emp.Address">
        <property name="city" value="PUNE"></property>
        <property name="street" value="MH"></property>
        <property name="pin" value="411013"></property>
    </bean>

    <bean id="customerDAO" class="org.mykong.springdb.Customer">

    </bean>

</beans>

这是我的客户端程序:

 public static void main( String[] args )
    {
        ApplicationContext context = 
            new ClassPathXmlApplicationContext("spring.xml");

        Customer customer = (Customer) context.getBean("customerDAO");
 System.out.println(customer.getBeanName());

    }

我的客户 bean:

     public class Customer implements BeanNameAware, BeanPostProcessor{

            private String name;
        private int age;

        protected Customer() {

        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    
        public String toString() {
            return "Customer id : "  + " , Name : "  + this.name + " , Age: " +     this.age;
        }

        public void setBeanFactory(BeanFactory arg0) throws BeansException {
        
        
        }

        private String beanName = null;
        public void setBeanName(String name) {
            this.beanName = name;
        }

        public String getBeanName() {
            return beanName;
        }

        public Object postProcessAfterInitialization(Object bean, String arg1)
                throws BeansException {
        
            System.out.println(" IN : postProcessAfterInitialization - bean         initialized" + bean.getClass() );
            return bean;
        }

        public Object postProcessBeforeInitialization(Object bean, String arg1)
                throws BeansException {

            System.out.println(" IN : postProcessBeforeInitialization "  );
            return bean;
        }
        }

问题1:因为我的spring.xml 有大约4 个bean 的配置。但是只有三个 bean 的输出(customerDAO 除外)。为什么?

问题2:如果我只想为 customerDAO 调用前后进程初始化方法怎么办? 可能就这么简单?我不知道。但请回答。提前致谢

输出:

IN : postProcessBeforeInitialization IN : postProcessAfterInitialization - bean 初始化类 org.springdemo.Triangle IN : postProcessBeforeInitialization IN : postProcessAfterInitialization - bean 初始化类 org.springdemo.emp.Address IN : postProcessBeforeInitialization IN : postProcessAfterInitialization - bean 初始化类 org.springdemo.emp.Employee 客户DAO

【问题讨论】:

    标签: spring


    【解决方案1】:

    BeanPostProcessor的目的是拦截其他bean的初始化。它不能拦截自己的初始化,因为 Spring 保证在调用其 post-process 方法时完全初始化 post-processor。

    如果您需要在这个 bean 初始化后执行操作,您可以执行以下操作之一:

    • 实现InitializingBean 并覆盖afterPropertiesSet
    • 使用&lt;bean&gt;init-method 属性指定要调用的方法的名称
    • @PostConstruct注解要调用的方法

    【讨论】:

    • 感谢您提供的信息,我真的不清楚您提到的第二点。但我的问题是我想限制调用方法 postProcessAfterInitialization() 和 postProcessBeforeInitialization() 到特定的 bean。
    猜你喜欢
    • 2012-06-22
    • 2017-02-11
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    相关资源
    最近更新 更多