初探Spring源码之Spring Bean的生命周期
写在前面的话:
学无止境,写博客纯粹是一种乐趣而已,把自己理解的东西分享出去,不意味全是对的,欢迎指正!
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
-
实例了一个 AnnotationConfigApplicationContext对象,Spring中出来注解Bean定义的类有两个:
AnnotationConfigApplicationContext和AnnotationConfigWebApplicationContex AnnotationConfigWebApplicationContext是AnnotationConfigApplicationContext的web版本,两者的用法以及对注解的处理方式几乎没有什么差别,通过分析这个类我们知道注册一个bean到spring容器有两种办法一、直接将注解Bean注册到容器中:(参考)public void register(Class<?>… annotatedClasses)但是直接把一个注解的bean注册到容器当中也分为两种方法:
1、在初始化容器时注册并且解析
2、也可以在容器创建之后手动调用注册方法向容器注册,然后通过手动刷新容器,使得容器对注册的注解Bean进行处理。
思考:为什么@profile要使用这类的第2种方法?
因为需要通过applicationContext.getEnvironment()这样的方式设置环境或者获取环境变量
二、通过扫描指定的包及其子包下的所有类扫描其实同上,也是两种方法,初始化的时候扫描,和初始化之后再扫描
public AnnotationConfigApplicationContext() {//0
/**
* 父类的构造方法
* 创建一个读取注解的Bean定义读取器
* 什么是bean定义?BeanDefinition
*/
this.reader = new AnnotatedBeanDefinitionReader(this); //1
//可以用来扫描包或者类,继而转换成bd
//但是实际上我们扫描包工作不是scanner这个对象来完成的
//是spring自己new的一个ClassPathBeanDefinitionScanner
//这里的scanner仅仅是为了程序员能够在外部调用AnnotationConfigApplicationContext对象的scan方法
this.scanner = new ClassPathBeanDefinitionScanner(this); //2
}
注意: * 初始化一个bean的读取和扫描器 * 何谓读取器和扫描器参考上面的属性注释 * 默认构造函数,如果直接调用这个默认构造方法,需要在稍后通过调用其register() * 去注册配置类(javaconfig),并调用refresh()方法刷新容器, * 触发容器对注解Bean的载入、解析和注册过程
-
实例化AnnotationConfigApplicationContext会先实例化父类GenericApplicationContext,然后初始化一个Bean工厂,DefaultListableBeanFactory
public GenericApplicationContext() {
this.beanFactory = new DefaultListableBeanFactory();
} -
1处,实例化一个BeanDefinition读取器 AnnotatedBeanDefinitionReader
/**
* 这里的BeanDefinitionRegistry registry是通过在AnnotationConfigApplicationContext
* 的构造方法中传进来的this
* 由此说明AnnotationConfigApplicationContext是一个BeanDefinitionRegistry类型的类
* 何以证明我们可以看到AnnotationConfigApplicationContext的类关系:
* GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry
* 看到他实现了BeanDefinitionRegistry证明上面的说法,那么BeanDefinitionRegistry的作用是什么呢?
* BeanDefinitionRegistry 顾名思义就是BeanDefinition的注册器
* 那么何为BeanDefinition呢?参考BeanDefinition的源码的注释
* @param registry
*/
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);
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
}
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
public static void registerAnnotationConfigProcessors(BeanDefinitionRegistry registry) {
registerAnnotationConfigProcessors(registry, null);
以上空壳方法,调用以下方法
public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
BeanDefinitionRegistry registry,
//主要是为了获得工厂对象,实例化AnnotationConfigApplicationContext的时候会调用父类
//GenericApplicationContext的构造器去实例化DefaultListableBeanFactory
//这里的registry其实际是GenericApplicationContext的子类实例,向上转型取得工厂的属性
//从而得到 beanFactory对象
DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
具体代码
private static DefaultListableBeanFactory unwrapDefaultListableBeanFactory(BeanDefinitionRegistry registry) {
if (registry instanceof DefaultListableBeanFactory) {
return (DefaultListableBeanFactory) registry;
}
else if (registry instanceof GenericApplicationContext) {
//这里在AnnotationConfigApplicationContext初始化的时候this()
//方法中调用了父类GenericApplicationContext的时候new了一个DefaultListableBeanFactory对象
//下面代码返回这个对象
return ((GenericApplicationContext) registry).getDefaultListableBeanFactory();
}
else {
return null;
}
}
这段代码主要是为解析是否含有需要排序的注解
if (beanFactory != null) {
if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
//AnnotationAwareOrderComparator主要能解析@Order注解和@Priority
beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
}
if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
//ContextAnnotationAutowireCandidateResolver提供处理延迟加载的功能
beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
}
}
org.springframework.context.annotation.internalConfigurationAnnotationProcessor这个是BeanDefinition
的一个beanName, 具体的类是ConfigurationClassPostProcessor,它在BeanDefinition中表现的是一个后置处理器(重要),是Spring的核心类之一。
Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);
//BeanDefinitio的注册,这里很重要,需要理解注册每个bean的类型
//org.springframework.context.annotation.internalConfigurationAnnotationProcessor
//这个是spring生命周期非常重要的一个类
if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
//需要注意的是ConfigurationClassPostProcessor的类型是BeanDefinitionRegistryPostProcessor
//而 BeanDefinitionRegistryPostProcessor 最终实现BeanFactoryPostProcessor这个接口
//因此需要对这两种类型的进行区分处理
//将一个类变成BeanDefinition
RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
def.setSource(source);
//这里是将BD注册到BeanFactory的BeanDefinitionMap容器中,并返回一个
//BeanDefinitionHolder对象
beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
}
实际上上面的代码和以下的代码主要往DefaultListableBeanFactory中的beanDefinitionMap 中放入以下所列举的6个后置处理器。其中ConfigurationClassPostProcessor是核心
0.org.springframework.context.annotation.ConfigurationClassPostProcessor(重要)
1.org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
2.org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor
3.org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
4.org.springframework.context.event.EventListenerMethodProcessor
5.org.springframework.context.event.DefaultEventListenerFactory
if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
//AutowiredAnnotationBeanPostProcessor 实现了 MergedBeanDefinitionPostProcessor
//MergedBeanDefinitionPostProcessor 最终实现了 BeanPostProcessor
RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
}
if (!registry.containsBeanDefinition(REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(RequiredAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
}
// Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
}
// Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition();
try {
def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
AnnotationConfigUtils.class.getClassLoader()));
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
}
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
}
if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
}
if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
}
疑问1:普通的对象是怎么变成BeanDefinition的呢?
//将一个类变成BeanDefinition
RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
疑问2:对象变成Beandefinition之后又是怎么注册到bean工厂的呢?
private static BeanDefinitionHolder registerPostProcessor(
BeanDefinitionRegistry registry, RootBeanDefinition definition, String beanName) {
definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
//将bd注册到工厂的BeanDefinitionMap中
registry.registerBeanDefinition(beanName, definition);
//BeanDefinitionHolder 只是封装参数使用
return new BeanDefinitionHolder(definition, beanName);
}
registry.registerBeanDefinition(beanName, definition);这句代码将Beandefinition注册到BeanFactory中
注意这里的registry注册器一直都是AnnotationConfigApplicationContext对象,该类中没有registerBeanDefinition() ,因此会调用父类GenericApplicationContext的registerBeanDefinition()方法注册Beandefinition。
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException {
this.beanFactory.registerBeanDefinition(beanName, beanDefinition);
}
调用org.springframework.beans.factory.support.DefaultListableBeanFactory#registerBeanDefinition()的方法执行以下逻辑
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException {
不管GenericBeanDefinition,还是RootBeanDefinition都继承了AbstractBeanDefinition,因此会执行以下逻辑
Assert.notNull(beanDefinition, "BeanDefinition must not be null");
//BeanDefinition的所有实现类都继承了AbstractBeanDefinition,因此会执行以下的检验逻辑
if (beanDefinition instanceof AbstractBeanDefinition) {
try {
((AbstractBeanDefinition) beanDefinition).validate();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
"Validation of bean definition failed", ex);
}
}
执行一些逻辑判断,打印一些log,这些不重要
//根据beanName从容器中取出BD,判断是否已经注册了的各种校验
BeanDefinition existingDefinition = this.beanDefinitionMap.get(beanName);
if (existingDefinition != null) {
//是否允许覆盖原有的bean 默认是不覆盖
if (!isAllowBeanDefinitionOverriding()) {
throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
"Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName +
"': There is already [" + existingDefinition + "] bound.");
}
else if (existingDefinition.getRole() < beanDefinition.getRole()) {
// e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
if (logger.isWarnEnabled()) {
logger.warn("Overriding user-defined bean definition for bean '" + beanName +
"' with a framework-generated bean definition: replacing [" +
existingDefinition + "] with [" + beanDefinition + "]");
}
}
else if (!beanDefinition.equals(existingDefinition)) {
if (logger.isInfoEnabled()) {
logger.info("Overriding bean definition for bean '" + beanName +
"' with a different definition: replacing [" + existingDefinition +
"] with [" + beanDefinition + "]");
}
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Overriding bean definition for bean '" + beanName +
"' with an equivalent definition: replacing [" + existingDefinition +
"] with [" + beanDefinition + "]");
}
}
//加入到beanDefinitionMap当中,正常第一次不会走到这里
this.beanDefinitionMap.put(beanName, beanDefinition);
}
else {
if (hasBeanCreationStarted()) {
// Cannot modify startup-time collection elements anymore (for stable iteration)
synchronized (this.beanDefinitionMap) {
//这里加锁的意义是防止正在创建的Bean被修改。
this.beanDefinitionMap.put(beanName, beanDefinition);
List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1);
updatedDefinitions.addAll(this.beanDefinitionNames);
updatedDefinitions.add(beanName);
this.beanDefinitionNames = updatedDefinitions;
if (this.manualSingletonNames.contains(beanName)) {
Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames);
updatedSingletons.remove(beanName);
this.manualSingletonNames = updatedSingletons;
}
}
}
初次注册BeanDefinition的会执行
else {
// Still in startup registration phase
//初次注册会执行这段逻辑,90%的注册会走到这里
this.beanDefinitionMap.put(beanName, beanDefinition);
this.beanDefinitionNames.add(beanName);
this.manualSingletonNames.remove(beanName);
}
this.frozenBeanDefinitionNames = null;
至此,实例化一个AnnotationBeanDefinitionReader会注册6个后置处理器,为什么注册这些后置处理器,有什么作用,会在后续的Spring源码解析中进行讲解。另外核心的类ConfigurationClassPostProcessor的作用是什么?
-
2处,实例化一个BeanDefinition 扫描器ClassPathBeanDefinitionScanner
BeanDefinition扫描器这里不做多介绍,因为这里的扫描器主要是为了给程序员自己定义一个类的扫描器,用来扫描我们自定义的加了注解类。