Spring BeanPostProcessor不会被调用其实现的bean,而是调用所有其他bean

时间:2015-04-06 05:48:21

标签: java spring spring-mvc spring-bean

我的 bean.xml

朋友们,我只是在学习春天。我在HelloWorld.java中实现了接口BeanPostProcessor。它为所有其他bean调用的方法,但不是它自己(Helloworld.java bean)

 <bean id="helloWorld" class="com.tutorialspoint.HelloWorld" >     </bean>  
   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.tutorialspoint.TextEditor">
      <property name="spellChecker" ref="spellChecker"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

Helloworld.java

public class HelloWorld implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String name)
            throws BeansException {

        System.out.println();       
        System.out.println(bean.getClass().getName() +"---------------"+name+"--->This is after bean initialized ");

        return bean;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String name)
            throws BeansException {

        System.out.println(bean.getClass().getName() +"---------------"+name+"----------->This is before bean initialized ");

        return bean;
    }
}

主程序

public class MainApp {
    public static void main(String arg[])throws Exception{
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");        

    }
    }

我的输出是

com.tutorialspoint.SpellChecker---------------spellChecker----------->This is before bean initialized 

com.tutorialspoint.SpellChecker---------------spellChecker--->This is after bean initialized 

com.tutorialspoint.TextEditor---------------textEditor----------->This is before bean initialized 

com.tutorialspoint.TextEditor---------------textEditor--->This is after bean initialized 

为什么没有为HelloWorld.java ..调用BeanPostProcessor接口方法,而是为其他没有实现BeanPostProcessor接口的无关bean调用..?

1 个答案:

答案 0 :(得分:4)

任何实现BeanPostprocessor的bean都可以作为所有bean的bean后处理器。有关详细信息,请参阅URL。明确提到ApplicationContexts can autodetect BeanPostProcessor beans in their bean definitions and apply them to any beans subsequently created. **Plain bean factories allow for programmatic registration of post-processors, applying to all beans created through this factory**.

您是否希望特定bean具有一些初始化代码,然后实现InitializingBean,如下所示

package org.studyspring.beanfactory    
public class HelloWorld implements InitializingBean {
        private String name;

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


        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println(this.getClass().getName() +"---------------name = "+name+"--->This is after bean initialized ");
        }
    }

在XML中添加此bean的条目

 <bean id="helloWorld" class="org.studyspring.beanfactory.HelloWorld">
        <property name="name" value="Shirish"/>
    </bean>

或者你可以为每个bean设置init和destroy方法,如下所示

package org.studyspring.beanfactory;

import org.springframework.beans.factory.InitializingBean;


public class HelloWorld1  {
    private String name;

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



    public void init() throws Exception {
        System.out.println(this.getClass().getName() +"---------------name = "+name+"--->This is before bean initialized");
    }

    public void destroy() throws Exception {
        System.out.println(this.getClass().getName() +"---------------name = "+name+"--->This is after bean destroyed ");
    }
}

在XML中注册您的bean,如下所示

<bean id="helloWorld1" class="org.studyspring.beanfactory.HelloWorld1" init-method="init" destroy-method="destroy">
    <property name="name" value="Shirish"/>
</bean>

此外,如果您遵循通用的做法来命名init&amp;销毁方法然后你可以在<beans>中定义这些方法,如下所示

<beans default-init-method="init" default-destroy-method="destroy"> 

上面有可用的init和destory方法的bean,它们将在初始化和破坏那些bean时被调用