Issue
my bean.xml
hi friends.., i am just learning spring . i implement the interface BeanPostProcessor in HelloWorld.java . its methods invoked for all other beans , but not itself(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;
}
}
main program
public class MainApp {
public static void main(String arg[])throws Exception{
AbstractApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
}
}
my output is
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
why BeanPostProcessor interface methods not invoked for HelloWorld.java ..,but invoked for other unrelated beans which is not implementing BeanPostProcessor interface ..?
Solution
any bean implementing BeanPostprocessor
works as bean postprocessor for all beans.Please refer URL for more details.It is clearly mentioned that 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**.
Do you want a specific bean having some initializing code then implement InitializingBean
as below
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 ");
}
}
add entry for this bean in XML
<bean id="helloWorld" class="org.studyspring.beanfactory.HelloWorld">
<property name="name" value="Shirish"/>
</bean>
or alternatively you can have init and destroy methods for each bean as below
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 ");
}
}
register your bean as below in XML
<bean id="helloWorld1" class="org.studyspring.beanfactory.HelloWorld1" init-method="init" destroy-method="destroy">
<property name="name" value="Shirish"/>
</bean>
Additionally if you follow common practice for naming init & destroy methods then you can define those methods in <beans>
as below
<beans default-init-method="init" default-destroy-method="destroy">
with above the beans which have init and destory methods available they will be called upon initialization and destructions of those beans
Answered By - Shirishkumar Bari
Answer Checked By - Willingham (JavaFixing Volunteer)