作用
BeanPostProcessor接口里面有下面两个方法:实现这两个方法可以在bean的初始化前后进行操作。
实现该接口的类必须注入到容器中才能生效
/**
Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.
*/
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
/**
Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.
*/
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
使用方法
先实现BeanPostProcessor接口,然后将其注入到容器中
/**
* 初始化前后进行操作
* 需要注入到容器中
* @author Floweryu
* @date 2022/3/6 20:53
*/
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println('postProcessBeforeInitialization... ' + bean + ' -> [' + beanName + ']');
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println('postProcessAfterInitialization... ' + bean + ' -> [' + beanName + ']');
return bean;
}
}
下面,针对不同的初始化的方法,都会在bean的初始化前后生效:
对于实现InitializingBean接口的类
@Component
public class Cat implements InitializingBean, DisposableBean {
public Cat() {
System.out.println('Cat construct....');
}
@Override
public void destroy() throws Exception {
System.out.println('Cat destroy....');
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println('Cat afterPropertiesSet....');
}
}
输出:postProcessBeforeInitialization…cofloweryu.examplbeaCat@44a7bfbc->[cat]CatafterPropertiesSet…postProcessAfterInitialization…cofloweryu.examplbeaCat@44a7bfbc->[cat]
对于使用@PostConstruct初始化的类:
@Component
public class Dog {
public Dog() {
System.out.println('Dog Construct....');
}
/**
* 对象创建并赋值后使用
*/
@PostConstruct
public void init() {
System.out.println('Dog @PostConstruct....');
}
/**
* 容器移除对象之前使用
*/
@PreDestroy
public void destroy() {
System.out.println('Dog @PreDestroy');
}
}
输出:postProcessBeforeInitialization…cofloweryu.examplbeaDog@306cf3ea->[dog]Dog@PostConstruct…postProcessAfterInitialization…cofloweryu.examplbeaDog@306cf3ea->[dog]
对于使用@Bean自定义的类
@ComponentScan('com.floweryu.example.bean')
@Configuration
public class MainConfigOfLifeCycle {
@Bean(initMethod = 'init', destroyMethod = 'destory')
public Car car() {
return new Car();
}
}
public class Car {
public Car() {
System.out.println('construct car...');
}
public void init() {
System.out.println('Car init....');
}
public void destory() {
System.out.println('Car destory....');
}
}
相关链接
@PostConstruct注解和@PreDestroy注解@Bean指定初始化和销毁方法
文章为作者独立观点,不代表股票交易接口观点