Reputation: 55
I am doing a BeanPostProcessor implementation. My Bean PostProcessor executing 3 times. I am getting post processor invoked 2 times extra before init method. I am unable to find why it is happening. Please help me.
My code and configuration files are as
BeanPostProcesssor implementatin
public class BeanPP implements BeanPostProcessor{
@Override
public Object postProcessBeforeInitialization(Object o, String string) throws BeansException {
System.out.println("---before initialization ------");
return o;
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public Object postProcessAfterInitialization(Object o, String string) throws BeansException {
System.out.println("----After initializaion ---- ");
return o;
//To change body of generated methods, choose Tools | Templates.
}
}
Customer Bean
public class Customer {
private String name;
public String getName() {
System.out.println("..getName.....");
return name;
}
public void setName(String name) {
System.out.println("..setName.....");
this.name = name;
}
@PostConstruct
public void init(){
System.out.println("....Bean is going though init method");
}
@PreDestroy
public void destory(){
System.out.println("....Bean is going to destroy.......");
}
}
Configuration Class
@Configuration
public class AppConfig {
@Bean(name="customer")
public Customer getCustomer(){
return new Customer();
}
@Bean
public BeanPP getPP(){
return new BeanPP();
}
}
Main Class
public class MainApp {
public static void main(String[] args) {
ApplicationContext appContext = new AnnotationConfigApplicationContext(AppConfig.class);
Customer customer = (Customer)appContext.getBean("customer");
customer.setName("test user ");
System.out.println(".Name is .."+customer.getName());
}
}
Output
---before initialization ------ ----After initializaion ---- ---before initialization ------ ----After initializaion ---- ---before initialization ------ ....Bean is going though init method ----After initializaion ---- ..setName..... ..getName..... .Name is ..test user
Upvotes: 1
Views: 1319
Reputation: 8334
Plain bean factories allow for programmatic registration of post-processors, applying to all beans created through this factory.
You have defined 3 beans, AppConfig, customer and PP. The bean post processor will be executed after every bean creation. If you have 3 beans it will be executed 3 times.
@Configuration
public class AppConfig {
@Bean(name="customer")
public Customer getCustomer(){
return new Customer();
}
@Bean
public BeanPP getPP(){
return new BeanPP();
}
}
@Configuration also define a bean, because it inheritance from @Component.
Upvotes: 3
Reputation: 7905
Can you put in postProcessBeforeInitialization
to print out also the Object o
class just to see which class calls the BeanPostProcessor (it may not be the Customer
class, but some other object that Spring initiates, even the BeanPostProcessor itself).
@Override
public Object postProcessBeforeInitialization(Object o, String string) throws BeansException {
System.out.println("---before initialization ------ " + o..getClass().getName());
return o;
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
Upvotes: 0