Spring Bean Post Processors
BeanPostProcessor gives you a way to do some operations before creating the spring bean and immediately after creating the
spring bean. This allows you to add some custom logic before and after spring bean creation. The BeanPostProcessors operate on bean (or object)
instances which means that the Spring IoC container instantiates a bean instance and then BeanPostProcessor interfaces do their work.
Notice that the init and destroy methods related to bean are different from bean post processors. BeanPostProcessors are
common for all beans. This example clearly shows the difference from them.
To implement bean post processor logic, we need to create a class which implements BeanPostProcessor interface and two of its
methods.
package com.java2novice.beans;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanInitProcessor implements BeanPostProcessor{
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("before initialization: "+beanName);
return bean;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("after initialization: "+beanName);
return bean;
}
}
|
Bean class:
package com.java2novice.beans;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class NetworkManager{
private HttpURLConnection connection;
private String urlStr;
public void setUrlStr(String urlStr){
this.urlStr = urlStr;
}
@PostConstruct
public void init(){
System.out.println("Inside init() method...");
URL obj;
try {
obj = new URL(this.urlStr);
//initialize http connection here
this.connection = (HttpURLConnection) obj.openConnection();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@PreDestroy
public void destroy(){
try{
System.out.println("Inside destroy() method...");
if(this.connection != null) {
connection.disconnect();
}
} catch(Exception ex){
}
}
public void readData(){
try {
int responseCode = this.connection.getResponseCode();
System.out.println("Response code: "+responseCode);
/**
* do your business logic here
*/
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|
Xml based configuration file, note that we registered BeanPostProcessor implementor class here:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<bean id="netManager" class="com.java2novice.beans.NetworkManager">
<property name="urlStr" value="http://www.google.com/search?q=java2novice"/>
</bean>
<bean class="com.java2novice.beans.MyBeanInitProcessor" />
</beans>
|
Spring bean demo class:
package com.java2novice.test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java2novice.beans.NetworkManager;
public class SpringDemo {
public static void main(String a[]){
String confFile = "applicationContext.xml";
ConfigurableApplicationContext context
= new ClassPathXmlApplicationContext(confFile);
NetworkManager networkMng = (NetworkManager) context.getBean("netManager");
networkMng.readData();
context.close();
}
}
|
|