Spring
源码 - 容器刷新#postProcessBeanFactory()
Spring
版本:Spring 5.3.13-release
# 1、postProcessBeanFactory()
AbstractApplicationContext#postProcessBeanFactory()
代码:
/**
* Modify the application context's internal bean factory after its standard
* initialization. All bean definitions will have been loaded, but no beans
* will have been instantiated yet. This allows for registering special
* BeanPostProcessors etc in certain ApplicationContext implementations.
* @param beanFactory the bean factory used by the application context
*/
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
}
可以看到,在AbstractApplicationContext#postProcessBeanFactory()
中是没有做任何事情的,这是Spring
预留给子类进行扩展使用的。模板方法设计模式。
而在SpringBoot
中的AnnotationConfigServletWebServerApplicationContext#postProcessBeanFactory()
是对这个方法进行了覆盖的:
/**
* override AbstractApplicationContext's postProcessBeanFactory()
* template method in Spring IOC refresh()
*
* @param beanFactory ConfigurableListableBeanFactory
*/
@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// 首先注册为 Web Application
super.postProcessBeanFactory(beanFactory);
// 扫描指定包下的 Bean 并将其进行注册
if (this.basePackages != null && this.basePackages.length > 0) {
this.scanner.scan(this.basePackages);
}
// 扫描指定注解的类下的 Bean 并将其注册
if (!this.annotatedClasses.isEmpty()) {
this.reader.register(ClassUtils.toClassArray(this.annotatedClasses));
}
}
我这里的
Spring Boot
源码版本为:2.6.2-release
GitHub源码地址:https://github.com/kapbc/kapcb-spring-source/tree/master/Spring-Framework-v5.3.13
备注:此文为笔者学习
Spring
源码的笔记,鉴于本人技术有限,文中难免出现一些错误,感谢大家批评指正。