随时记录spring一些零碎的东西,随时更新!
Autowire
开启注入支持,配置:
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
AutowiredAnnotationBeanPostProcessor处理器实现了MergedBeanDefinitionPostProcessor,源码在:
// AbstractAutowireCapableBeanFactory
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) {
....
synchronized (mbd.postProcessingLock) {
if (!mbd.postProcessed) {
// 实例化后,属性填充和初始化前,可以改变bean定义
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
mbd.postProcessed = true;
}
}
....
}
protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class beanType, String beanName)
throws BeansException {
try {
// 应用merge处理器
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof MergedBeanDefinitionPostProcessor) {
MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp;
bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName);
}
}
}
catch (Exception ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Post-processing failed of bean type [" + beanType + "] failed", ex);
}
}
PostConstruct和PreDestroy
初始化 bean 和销毁前注解。开启注入支持,配置:
<bean class="org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor" />
CommonAnnotationBeanPostProcessor处理器实现了MergedBeanDefinitionPostProcessor。处理同上面Autowire。