spring生命周期是spring IOC的管理过程。本文主要是针对spring有一定了解的读者,如果还不知道什么是spring建议先去官方(spring官网)学习,由于spring源码非常复杂,本文通过主流程的方式来解读源码。想详细了解spring原理请关注博主的spring系列文章。
本文从源码的角度分析spring从加载到生成Bean的整个生命周期,废话不多说直接上源码:
@Configuration
public class TestConfig {
@Bean
public String name() {
return "Hello Spring.";
}
}
public class TestMain {
public static void main(String[] args) {
// 创建SpringIOC容器
ApplicationContext acx = new AnnotationConfigApplicationContext(TestConfig.class);
// 通过Bean名称获取Bean
Object name = acx.getBean("name");
System.out.println(name);
}
}
public AnnotationConfigApplicationContext() {
StartupStep createAnnotatedBeanDefReader = this.getApplicationStartup().start("spring.context.annotated-bean-reader.create");
// AnnotatedBeanDefinition的解析器
this.reader = new AnnotatedBeanDefinitionReader(this);
createAnnotatedBeanDefReader.end();
// 类路劲下扫描Bean的扫描器
this.scanner = new ClassPathBeanDefinitionScanner(this);
}
public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry) {
this(registry, getOrCreateEnvironment(registry));
}
public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
Assert.notNull(environment, "Environment must not be null");
this.registry = registry;
this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);
/**
* 注册内部BeanFactoryPostProcessor
*/
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
}
/**
* 配置类加载Bean
*/
public AnnotationConfigApplicationContext(Class<?>... componentClasses) {
/**
* 调用无参构造器初始化AnnotatedBeanDefinition的解析器和 Bean的扫描器
* AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry); 加载内部逻辑处理Bean
*/
this();
/**
* 将componentClasses配置类注册为AnnotatedBeanDefinition对象
*/
register(componentClasses);
/**
* 刷新Bean,也是Bean中最核心的方法没有之一
* 想理解要Bean,必须了解该类型
*/
refresh();
}
// 刷新Bean的加载逻辑
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
// Prepare this context for refreshing.
/**
* 准备上下文刷新工作,如设置初始值
*/
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
/**
* 告诉子类刷新内部beanFactory,返回Bean工厂
*/
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
/**
* 准备beanFactory,以便于上下文中使用
*/
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
/**
* 允许在上下文子类中对bean工厂进行后处理。
*/
postProcessBeanFactory(beanFactory);
/**
* 开启处理PostProcessors步骤器
*/
StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
// Invoke factory processors registered as beans in the context.
/**
* 调用BeanFactory的后置处理器
*/
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
/**
*
* 注册拦截bean创建的bean处理器
*/
registerBeanPostProcessors(beanFactory);
/**
* 处理PostProcessors步骤器
*/
beanPostProcess.end();
// Initialize message source for this context.
/**
* 初始化MessageSource
*/
initMessageSource();
// Initialize event multicaster for this context.
/**
* 初始化Application监听器的管理Bean(ApplicationEventMulticaster)
*/
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
/**
* 模板模式,刷新Bean的操作,由子类实现具体逻辑
*/
onRefresh();
// Check for listener beans and register them.
/**
* 检查和注册监听器
*/
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
/**
* 实例化所有(非惰性初始化)单例
*/
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
/**
* 发布相应的事件
*/
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// 出现异常销毁Bean
destroyBeans();
// 重置'active' 标志.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
contextRefresh.end();
}
}
}
加载Bean执行逻辑:
到这里Bean整个生命周期就执行完成了。其中invokeBeanFactoryPostProcessors(beanFactory) 和finishBeanFactoryInitialization(beanFactory) 方法是整个流程的核心,下面对他们详细分析。
在执行nvokeBeanFactoryPostProcessors(beanFactory)方法之前都是做准备工作,该方法是执行对已加载的Bean的BeanFactoryPostProcessory类型对象进行调用,换句话说:该方法就是加载所有满足Bean条件的java类,将其解析为BeanDefinition对象注册到beanFactory。由于该方法逻辑比较复杂单独用一篇博文来讲解,请移步。
该方法是对单例Bean的进行初始化,直接上源码:
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
// 初始化service类型转换器.
if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
beanFactory.setConversionService(
beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
}
/**
* 如果之前没有注册BeanFactoryPostProcessor(如PropertySourcesPlaceHolderConfigurerBean),
* 请注册默认嵌入值解析程序:此时,主要用于注释属性值中的解析
*/
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
// 尽早初始化LoadTimeWeaverAware bean,以便尽早注册其转换器。
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
/**
* 先加载LoadTimeWeaverAware类型的Bean
*/
for (String weaverAwareName : weaverAwareNames) {
/**
* 初始化Bean
*/
getBean(weaverAwareName);
}
// Stop using the temporary ClassLoader for type matching.
/**
* 停止使用临时类加载器进行类型匹配。
*/
beanFactory.setTempClassLoader(null);
// Allow for caching all bean definition metadata, not expecting further changes.
/**
* 允许缓存所有bean定义元数据,不需要进一步更改
*/
beanFactory.freezeConfiguration();
// Instantiate all remaining (non-lazy-init) singletons.
/**
* 初始化单例的Bean(非懒加载)
*/
beanFactory.preInstantiateSingletons();
}
beanFactory.preInstantiateSingletons() 是真正执行加载Bean的逻辑
@Override
public void preInstantiateSingletons() throws BeansException {
if (logger.isTraceEnabled()) {
logger.trace("Pre-instantiating singletons in " + this);
}
// Iterate over a copy to allow for init methods which in turn register new bean definitions.
// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
// Trigger initialization of all non-lazy singleton beans...
for (String beanName : beanNames) {
/**
* 获取合并Bean
*/
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
/**
* 处理FactoryBean类型的Bean
*/
if (isFactoryBean(beanName)) {
/**
* 通过Bean名称获取FactoryBean
* FactoryBean 默认在真正使用了该Bean时才调用getObject()方法,创建
*/
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
if (bean instanceof FactoryBean) {
FactoryBean<?> factory = (FactoryBean<?>) bean;
boolean isEagerInit;
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = AccessController.doPrivileged(
(PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit,
getAccessControlContext());
}
else {
isEagerInit = (factory instanceof SmartFactoryBean &&
((SmartFactoryBean<?>) factory).isEagerInit());
}
/**
* 如果FactoryBean是SmartFactoryBean类型,且isEagerInit为True,则立即调用getObject方法
*/
if (isEagerInit) {
getBean(beanName);
}
}
}
else {
/**
* 不是FactoryBean类型的Bean立即初始化
*/
getBean(beanName);
}
}
}
根据源码发现最终都是通过执行getBean(beanName)来初始化Bean。
该方法是spring中重要的方法之一,主要功能如下:
由于该方法代码实现非常复杂,博主将用一篇文章详细讲解,请移步。
根据源码分析spring的生命周期包含注册Bean和实例化Bean两大阶段:
第一阶段:注册Bean
1. 初始化spring上下文包括,BeanFactory,扫描器,BeanDefinition解析器等
2. 加载当前定义的Bean和内部Bean
3. 执行BeanFactoryPostProcessor接口,查询所有满足Bean的java对象,将其转换为BeanDefinition对象注册到BeanFactory中
4. 根据已加载的Bean寻找所有BeanPostProcessors对象注册到BeanFactory中
5. 初始化上下文资源MessageSource
6. 注册监听器
7. 初始化单例Bean
8. 触发相关事件
第二阶段:实例化Bean
1. 通过BeanDefinition创建出Bean实例instance,如果该实例构造函数中需要注入Bean也在该步骤完成
2. 执行@PostConstruct的方法
3. 通过实例进行反向依赖注入 ,针对属性值和set方法注入
4. 执行BeanAware的相关接口,初始化方法,和BeanPostProcessors
5. 返回Bean实例对象,如果是单例则缓存Bean
6. 销毁Bean