Spring BeanPostProcessor 未调用其实现的 bean,但调用所有其他 bean

Spring BeanPostProcessor not get called its implemented bean , but invoke for all other beans

我的bean.xml

朋友们好..,我正在学习spring。我在 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 调用 ..?

任何实现 BeanPostprocessor 的 bean 都可以作为所有 beans.Please 的 bean 后处理器,请参考 URL 更多 details.It 明确提到 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 和 destroy 方法的惯例,那么您可以在 <beans> 中定义这些方法,如下所示

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

上面的 bean 具有可用的 init 和 destory 方法,它们将在这些 bean 的初始化和销毁​​时被调用