前言
bean创建时的扩展中讲到各个阶段有不同的增强器,那我们来看看具体有哪些呢
上图-processon
BeanPostProcessor
实例化前增强
实例化后增强
赋值增强器
初始化前增强
初始化后增强
实例化增强器
- ConfigurationClassPostProcessor.ImportAwareBeanPostProcessor
- InfrastructureAdvisorAutoProxyCreator
- CommonAnnotationBeanPostProcessor
- AutowiredAnnotationBeanPostProcessor
赋值增强器
- ConfigurationClassPostProcessor.ImportAwareBeanPostProcessor
- InfrastructureAdvisorAutoProxyCreator
- CommonAnnotationBeanPostProcessor
- AutowiredAnnotationBeanPostProcessor
初始化增强器
- ApplicationContextAwareProcessor
- ConfigurationClassPostProcessor.ImportAwareBeanPostProcessor
- PostProcessorRegistrationDelegate.BeanPostProcessorChecker
- ConfigurationPropertiesBindingPostProcessor
- InfrastructureAdvisorAutoProxyCreator
- CommonAnnotationBeanPostProcessor
- AutowiredAnnotationBeanPostProcessor
- ApplicationListenerDetector
实例化与赋值增强器是相同的,实例化与初始化存在4个相同的增强器,因为他们都实现了InstantiationAwareBeanPostProcessor接口
常规增强器
CommonAnnotationBeanPostProcessor
处理@Resource、@PostConstruct、@PreDestroy注解
AutowiredAnnotationBeanPostProcessor
处理@Autowired、@Value、以及所有实现java.lang.annotation.Annotation接口的注解,所有注解、自定义注解默认隐式实现了该接口
InfrastructureAdvisorAutoProxyCreator
将bean封装为代理对象
ApplicationListenerDetector
检测自定义ApplicationListener并添加到监听列表中
代码示例演示Bean属性值变化顺序
设计想法:在每个BeanProcessor间加入一个DebugProcessor观察经过上个Processor处理后bean属性值变化
重写refresh
@SpringBootApplication
public class SpringLearnApplication {
public static void main(String[] args) {
MySpringApplication mySpringApplication = new MySpringApplication(SpringLearnApplication.class);
mySpringApplication.run(args);
}
public static class MySpringApplication extends SpringApplication {
public MySpringApplication(Class<?>... primarySources) {
super(primarySources);
}
@Override
protected ConfigurableApplicationContext createApplicationContext() {
return new MyApplicationContext();
}
}
public static class MyApplicationContext extends AnnotationConfigApplicationContext {
private final Object startupShutdownMonitor = new Object();
@Override
public void refresh() throws BeansException, IllegalStateException {
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Register debug processor that watches the changes of bean properties
registerDebugProcessor(beanFactory);
beanPostProcess.end();
}
private void registerDebugProcessor(ConfigurableListableBeanFactory beanFactory){
List<BeanPostProcessor> beanPostProcessors = ((DefaultListableBeanFactory) beanFactory).getBeanPostProcessors();
BeanPostProcessor debugProcessor = null;
for (BeanPostProcessor beanPostProcessor : beanPostProcessors) {
if (beanPostProcessor instanceof OneBean.DebugProcessor) {
debugProcessor = beanPostProcessor;
}
}
if (debugProcessor != null) {
beanPostProcessors.remove(debugProcessor);
}
int size = beanPostProcessors.size();
for (int i = 0; i < size; i++) {
int index = 2 * i + 1;
beanPostProcessors.add(index, debugProcessor);
}
}
}
}
所有逻辑原封不动的照抄下来,只在registerBeanPostProcessors后添加了一个registerDebugProcessor 在上一篇文章代码基础上添加DebugProcessor
@Component
public static class DebugProcessor implements InstantiationAwareBeanPostProcessor {
private int lastHashCode = 0;
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInstantiation(beanClass, beanName);
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
if (beanName.equals(OneBean.beanName)) {
if (bean.hashCode() != lastHashCode) {
LOGGER.info("value change--" + valueIndex + ":" + JSON.toJSONString(bean));
valueIndex++;
lastHashCode = bean.hashCode();
}
}
return InstantiationAwareBeanPostProcessor.super.postProcessAfterInstantiation(bean, beanName);
}
@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
if (beanName.equals(OneBean.beanName)) {
if (bean.hashCode() != lastHashCode) {
LOGGER.info("value change--" + valueIndex + ":" + JSON.toJSONString(bean));
valueIndex++;
lastHashCode = bean.hashCode();
}
}
return InstantiationAwareBeanPostProcessor.super.postProcessProperties(pvs, bean, beanName);
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals(OneBean.beanName)) {
if (bean.hashCode() != lastHashCode) {
LOGGER.info("value change--" + valueIndex + ":" + JSON.toJSONString(bean));
valueIndex++;
lastHashCode = bean.hashCode();
}
}
return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals(OneBean.beanName)) {
if (bean.hashCode() != lastHashCode) {
LOGGER.info("value change--" + valueIndex + ":" + JSON.toJSONString(bean));
valueIndex++;
lastHashCode = bean.hashCode();
}
}
return InstantiationAwareBeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
}
观察结果: