BeanPostProcessor


What is it for?

Execute a block of code for each and every bean that is initialised.

  • Bean Post Processor is a separate class
  • Spring executes the code in the Post Processor after initializing each and every bean

Example

Print the Name of Every Bean that is Initialized

DisplayNameBeanPostProcessor

package com.skills421.examples.spring;

public class DisplayNameBeanPostProcessor implements BeanPostProcessor
{
public Object postProcessBeforeInitialization(Object bean, String beanName)
{
return bean;
}

public Object postProcessAfterInitialization(Object bean, String beanName)
{
System.out.println("Initialized Bean: "+beanName);
return bean;
}
}

Register the BeanPostProcessor with Spring

Spring.xml

<bean class="com.skills421.examples.spring.DisplayNameBeanPostProcessor" />
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s