Spring - BeanPostProcessor接口

时间:2012-10-02 05:50:06

标签: spring

我刚开始学习春天。在实施示例程序时,我遇到了一个问题,并考虑在此论坛中进行检查以获得答案。

Follwoing是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 initializedclass org.springdemo.Triangle  IN:postProcessBeforeInitialization  IN:postProcessAfterInitialization - bean initializedclass org.springdemo.emp.Address  IN:postProcessBeforeInitialization  IN:postProcessAfterInitialization - bean initializedclass org.springdemo.emp.Employee customerDAO

1 个答案:

答案 0 :(得分:3)

BeanPostProcessor的目的是拦截其他bean的初始化。它无法拦截自己的初始化,因为Spring保证后处理器在调用后处理方法时完全初始化。

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

  • 实施InitializingBean并覆盖afterPropertiesSet
  • 使用init-method的{​​{1}}属性指定要调用的方法的名称
  • 使用<bean>
  • 注释要调用的方法