BeanPostProcessor Interface Overview
Bean BeanPostProcessor postprocessor also known, which is the interface defined in the Spring, the container creation process Spring BeanPostProcessor two methods defined in (specifically, the front and rear Bean initialization) will callback.
BeanPostProcessor specific code is as follows:
public interface BeanPostProcessor {
//bean初始化之前
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
//bean初始化之后
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
postProcessBeforeInitialization method calls back before the initialization method of each bean object calls; postProcessAfterInitialization method will be called back after the initialization method of each bean object calls.
These two methods were performed before and after the init method, you need a little attention, we define a class implements BeanPostProcessor, the default is the entire Spring container will all the bean processing.
Since all the processing is the default, then how do we confirm a specific bean we need to deal with it?
You can see the method takes two parameters. Object type respectively and String, the first parameter is an instance of each bean, the second parameter is the value of each bean name or id attribute. So we can second argument, we have to identify specific bean to be processed.
BeanPostProcessor example
Creating User Objects
public class User implements InitializingBean, DisposableBean {
public User() {
System.out.println("实例化User构造方法...");
}
public void initUser() {
System.out.println("User初始化方法initUser...");
}
public void destroyUser() {
System.out.println("User销毁方法destroyUser...");
}
@Override
public void destroy() throws Exception {
System.out.println("调用实现DisposableBean的destroy方法....");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("调用实现InitializingBean的afterPropertiesSet方法......");
}
}
BeanPostProcessor implementation class
public class UserPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
// 这边只做简单打印 原样返回bean
System.out.println("postProcessBeforeInitialization===="+beanName);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
// 这边只做简单打印 原样返回bean
System.out.println("postProcessAfterInitialization===="+beanName);
return bean;
}
}
Profile UserConfig
@Configuration
public class UserConfig {
@Bean(initMethod = "initUser",destroyMethod = "destroyUser")
public User user() {
return new User();
}
@Bean
public UserPostProcessor userPostProcessor() {
return new UserPostProcessor();
}
}
Test category
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(UserConfig.class);
((AnnotationConfigApplicationContext) applicationContext).close();
}
}
Results of the:
实例化User构造方法...
postProcessBeforeInitialization====user
调用实现InitializingBean的afterPropertiesSet方法......
User初始化方法initUser...
postProcessAfterInitialization====user
19:57:17.700 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2437c6dc, started on Tue Oct 22 19:57:17 CST 2019
调用实现DisposableBean的destroy方法....
User销毁方法destroyUser...