- 1.概述
- 2.测试
- 3.后置处理器的实现的顺序
生命周期后置处理器: BeanPostProcessor, BeanFactoryPostProcessor
生命周期后置处理: InitializingBean, DisposableBean
后置处理器会传入bean, 在于改变, 而InitialzingBean在于额外处理
这里通过实现类实现他们
BeanPostProcessor
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyBeanPostProcessor postProcessBeforeInitialization");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyBeanPostProcessor postProcessAfterInitialization");
return bean;
}
}
InstantiationAwareBeanPostProcessor
@Component
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
System.out.println("MyInstantiationAwareBeanPostProcessor postProcessBeforeInstantiation");
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
System.out.println("MyInstantiationAwareBeanPostProcessor postProcessAfterInstantiation");
return true;
}
@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
System.out.println("MyInstantiationAwareBeanPostProcessor postProcessProperties");
return null;
}
}
MergedBeanDefinitionPostProcessor
@Component
public class MyMergedBeanDefinitionPostProcessor implements MergedBeanDefinitionPostProcessor {
@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
System.out.println("MergedBeanDefinitionPostProcessor postProcessMergedBeanDefinition");
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyMergedBeanDefinitionPostProcessor postProcessBeforeInitialization" + beanName);
return null;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyMergedBeanDefinitionPostProcessor postProcessAfterInitialization" + beanName);
return null;
}
}
SmartInstantiationAwareBeanPostProcessor
@Component
public class MySmartInstantiationAwareBeanPostProcessor implements SmartInstantiationAwareBeanPostProcessor {
@Override
public Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
System.out.println("MySmartInstantiationAwareBeanPostProcessor predictBeanType");
return null;
}
@Override
public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {
System.out.println("MySmartInstantiationAwareBeanPostProcessor determineCandidateConstructors");
return null;
}
@Override
public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
System.out.println("MySmartInstantiationAwareBeanPostProcessor getEarlyBeanReference");
return bean;
}
}
BeanFactoryPostProcessor
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("MyBeanFactoryPostProcessor postProcessBeanFactory");
}
}
BeanDefinitionRegistryPostProcessor
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
System.out.println("MyBeanDefinitionRegistryPostProcessor postProcessBeanDefinitionRegistry");
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("MyBeanDefinitionRegistryPostProcessor postProcessBeanFactory");
}
}
InitializingBean
@Component
public class MyInitializingBean implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("MyInitializingBean afterPropertiesSet");
}
}
运行的顺序为
MyBeanDefinitionRegistryPostProcessor postProcessBeanDefinitionRegistry
MyBeanDefinitionRegistryPostProcessor postProcessBeanFactory
MyBeanFactoryPostProcessor postProcessBeanFactory
MySmartInstantiationAwareBeanPostProcessor predictBeanType
MyInstantiationAwareBeanPostProcessor postProcessBeforeInstantiation
MySmartInstantiationAwareBeanPostProcessor determineCandidateConstructors
MergedBeanDefinitionPostProcessor postProcessMergedBeanDefinition
MyInstantiationAwareBeanPostProcessor postProcessAfterInstantiation
MyInstantiationAwareBeanPostProcessor postProcessProperties
MyBeanPostProcessor postProcessBeforeInitialization
MyMergedBeanDefinitionPostProcessor postProcessBeforeInitializationmainConfig
MyInitializingBean afterPropertiesSet
MyBeanPostProcessor postProcessAfterInitialization
MyMergedBeanDefinitionPostProcessor postProcessAfterInitializationmainConfig
3.后置处理器的实现的顺序
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)