1、什么是Bean
1、java面向对象(oop),对象均有方法和属性,那么就需要对对象进行实例化来调用方法和属性(即实例化)
2、Spring bean 是被实例化的,组装的及被spring容器管理的java对象
3、Spring容器会自动完成@Bean对象的实例化,创建对象之间的协作关系的行为被称为:装配(wiring),即依赖注入的本质
1-1、bean的使用(注解形式)
1、bean使用:即将在xml或注解配置的bean拿来使用,完成属性、方法的组装,如:@Autowired、@Resource。可通过ByType(@Autowired)和ByName(@Resource)的方式获取bean
2、bean注册:@Component , @Repository , @ Controller , @Service , @Configration这些注解都是把你要实例化的对象转化成一个Bean,放在IoC容器中,等需要用的时候通过上述注解调用。
1-2、bean的注入方式
1、使用属性的setter方法注入
2、使用构造器注入
3、使用Filed注入(用于注解方式)
1-3、bean作用域
作用域 | 描述 |
singleton | 在spring ioc容器中仅存在一个bean实例,Bean以单例方式存在,默认值 |
prototype | 每次从容器中调用bean的时候都会创建一个新的实例(克隆) |
request | 每次HTTP请求都会创建一个新的Bean,只适用于web模块 |
session | 同一个HTTP Session共享一个Bean,不同的Session创建新的bean |
global Session | 在一个全局的HTTP Session中,容器会返回该Bean的同一个实例,仅在使用portletContext时有效 |
2、Spring bean 的加载过程
- 获取 BeanName,对传入的 name 进行解析,转化为可以从 Map 中获取到 BeanDefinition 的 bean name。
- 合并 Bean 定义,对父类的定义进行合并和覆盖,如果父类还有父类,会进行递归合并,以获取完整的 Bean 定义信息。
- 实例化,使用构造或者工厂方法创建 Bean 实例。
- 属性填充,寻找并且注入依赖,依赖的 Bean 还会递归调用 getBean 方法获取。
- 初始化,调用自定义的初始化方法。
- 获取最终的 Bean,如果是 FactoryBean 需要调用 getObject 方法,如果需要类型转换调用 TypeConverter 进行转化。
2-1、细节分析
1、转换BeanName
在解析完配置后会创建Map,使用BeanName作为key。见源码DefaultListableBeanFactory
private final Map beanDefinitionMap = new ConcurrentHashMap(256);
- BeanFactory.getBean中传入name,可能会出以下几种情况
- bean name:直接获取到定义的BeanDefiniton
- alias name: 别名,需要转化
- factorybean name带&前缀:通过他获取到BeanDefinition的时候需要去除&前缀
2、合并RootBeanDefinition
通过从配置文件读取到的BeanDefinition是GenericBeanDefinition,它记录了一些当前类声明的属性和构造参数,但对于父类只用了一个parentName来记录
package org.springframework.beans.factory.support;import org.springframework.beans.factory.config.BeanDefinition;import org.springframework.lang.Nullable;public class GenericBeanDefinition extends AbstractBeanDefinition { @Nullable private String parentName; .... }
通过执行会发现一个问题,在后续实例化bean的时候,使用的BeanDefinition是RootBeanDefinition类并非GenericBeanDefinition,这是为何?
GenericBeanDefinition在存在继承关系下,定义的信息会存在不完整
- 如果不存在继承关系,GenericBeanDefinition 存储的信息是完整的,可以直接转化为 RootBeanDefinition。
- 如果存在继承关系,GenericBeanDefinition 存储的是 增量信息 而不是 全量信息。
为了能够正确初始化对象,需要完整的信息才行。需要递归 合并父类的定义:
3、循环依赖
1、什么是循环依赖
举个例子,这里有三个类 A、B、C,然后 A 关联 B,B 关联 C,C 又关联 A,这就形成了一个循环依赖。如果是方法调用是不算循环依赖的,循环依赖必须要持有引用
2、循环依赖注入的时机
- 构造器循环依赖。依赖的对象是通过构造器传入的,发生在实例化 Bean 的时候。
- 设值循环依赖。依赖的对象是通过 setter 方法传入的,对象已经实例化,发生属性填充和依赖注入的时候。
如果是构造器循环依赖,本质上是无法解决的。比如我们准备调用 A 的构造器,发现依赖 B,于是去调用 B 的构造器进行实例化,发现又依赖 C,于是调用 C 的构造器去初始化,结果依赖 A,整个形成一个死结,导致 A 无法创建。
如果是设值循环依赖,Spring 框架只支持单例下的设值循环依赖。Spring 通过对还在创建过程中的单例,缓存并提前暴露该单例,使得其他实例可以引用该依赖
3、原型模式下的循环依赖
/** Names of beans that are currently in creation */ private final ThreadLocal prototypesCurrentlyInCreation = new NamedThreadLocal("Prototype beans currently in creation");
Spring 不支持原型模式的任何循环依赖。检测到循环依赖会直接抛出 BeanCurrentlyInCreationException 异常。
if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } /** * Return whether the specified prototype bean is currently in creation * (within the current thread). * @param beanName the name of the bean */ protected boolean isPrototypeCurrentlyInCreation(String beanName) { Object curVal = this.prototypesCurrentlyInCreation.get(); return (curVal != null && (curVal.equals(beanName) || (curVal instanceof Set && ((Set>) curVal).contains(beanName)))); }
3、单例模式下的构造循环依赖
/** Names of beans that are currently in creation */ /** * 当前正在创建的bean; * 一般来说,外层都是调用getBean(Class) * 一开始,会去检查三级缓存里有没有;没有的话,一般就要创建,这个是创建前才去add的。 * key是bean name;value是true */ private final Set singletonsCurrentlyInCreation = Collections.newSetFromMap(new ConcurrentHashMap(16));
单例模式下,构造函数的循环依赖无法解决,但设值循环依赖是可以解决的。其流程如下
4、核心处理三级缓存
/** 1级缓存 Cache of singleton objects: bean name to bean instance. */ private final Map singletonObjects = new ConcurrentHashMap<>(256); /** 2级缓存 Cache of early singleton objects: bean name to bean instance. */ private final Map earlySingletonObjects = new HashMap<>(16); /** 3级缓存 Cache of singleton factories: bean name to ObjectFactory. */ private final Map> singletonFactories = new HashMap<>(16);
使用一级缓存时此时是不完整的bean,field里的bean还未被设置;使用二级缓存时在aop情形下,注入到其他bean,不是最终的代理对象,而是使用原始对象,spring将抛出异常,当使用三级缓存时,不但存储了原始bean还存在一个工厂对象,通过工厂对象可以拿到最终形态的代理后的对象
4、创建实例
得到完整的RootBeanDefintion后,可以通过这份定义的信息来实例具体的bean,具体实例见
AbstractAutowireCapableBeanFactory.createBeanInstance ,返回 Bean 的包装类 BeanWrapper,一共有三种策略:
- 使用工厂方法创建,instantiateUsingFactoryMethod 。
- 使用有参构造函数创建,autowireConstructor。
- 使用无参构造函数创建,instantiateBean。
5、注入属性
主要的处理环节:
- 应用 InstantiationAwareBeanPostProcessor 处理器,在属性注入前后进行处理。假设我们使用了 @Autowire 注解,这里会调用到 AutowiredAnnotationBeanPostProcessor 来对依赖的实例进行检索和注入的,它是 InstantiationAwareBeanPostProcessor 的子类。
- 根据名称或者类型进行自动注入,存储结果到 PropertyValues 中。
- 应用 PropertyValues,填充到 BeanWrapper。这里在检索依赖实例的引用的时候,会递归调用 BeanFactory.getBean 来获得。
6、初始化
Spring 提供了 Aware 系列接口来解决这个问题。比如有这样的 Aware:
- BeanFactoryAware,用来获取 BeanFactory。
- ApplicationContextAware,用来获取 ApplicationContext。
- ResourceLoaderAware,用来获取 ResourceLoaderAware。
- ServletContextAware,用来获取 ServletContext。
7、BeanDefinition
org.springframework.beans.factory.config.BeanDefinition接口的方法再简单列一下:
public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { // scope:单例 String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON; // scope:prototype String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE; // 角色:属于应用程序的bean int ROLE_APPLICATION = 0; // 角色:支持?不甚了解,先跳过 int ROLE_SUPPORT = 1; // 角色:属于框架自身的bean int ROLE_INFRASTRUCTURE = 2; // parent bean的名字 String getParentName(); void setParentName(String parentName); // 核心属性,此为bean的class名称 String getBeanClassName(); void setBeanClassName(String beanClassName); // 核心属性,本属性获取工厂bean的名称,getFactoryMethodName获取工厂方法的名称,配合使用,生成 // 本bean String getFactoryBeanName(); void setFactoryBeanName(String factoryBeanName); String getFactoryMethodName(); void setFactoryMethodName(String factoryMethodName); //scope,bean是单例的,还是每次new一个(prototype),就靠它了 String getScope(); void setScope(String scope); // 懒bean?默认情况下,都是容器启动时,初始化;如果设置了这个值,容器启动时不会初始化,首次getBean // 时才初始化 boolean isLazyInit(); void setLazyInit(boolean lazyInit); // 在本bean初始化之前,需要先初始化的bean;注意,这里不是说本bean依赖的其他需要注入的bean String[] getDependsOn(); void setDependsOn(String[] dependsOn); // 是否够资格作为自动注入的候选bean。。。如果这里返回false,那就连自动注入的资格都没得 boolean isAutowireCandidate(); void setAutowireCandidate(boolean autowireCandidate); // 当作为依赖,要注入给某个bean时,当有多个候选bean时,本bean是否为头号选手 boolean isPrimary(); void setPrimary(boolean primary); // 通过xml 方式定义bean时,通过来定义构造器的参数,这里即:获取构造器参数 ConstructorArgumentValues getConstructorArgumentValues(); // 通过xml 方式定义bean时,通过 这种方 式来注入依赖,这里即:获取property注入的参数值 MutablePropertyValues getPropertyValues(); // 是否单例 boolean isSingleton(); // 是否prototype boolean isPrototype(); // 是否为抽象的,还记得方式定义的时候,可以这样指定吗? boolean isAbstract(); // 获取角色 int getRole(); // 获取描述 String getDescription(); String getResourceDescription(); // 未知。。。 BeanDefinition getOriginatingBeanDefinition();}
3、Spring Bean 生命周期
1.实例化BeanFactoryPostProcessor:处理的对象是BeanFactory级别
2.实例化BeanPostProcessor实现类
3.实例化InstantiationAwareBeanPostProcessorAdapter实现类,注:该类是BeanPostProcessor的扩展
4.执行InstantiationAwareBeanPostProcessorAdapter类的postProcessBeforeInstantiation方法
5.Bean的构造方法
6.执行InstantiationAwareBeanPostProcessorAdapter类的postProcessPropertyValues
7.为Bean注入属性,即依赖注入
8.调用BeanNameAware的setBeanName方法
9.调用BeanNameAware的setBeanFactory方法
10.执行BeanPostProcessor的后置处理器,postProcessAfterInitialization方法
11.调用InitializingBean的afterPropertiesSet方法
12.调用bean的init-method初始化方法
13.执行BeanPostProcessor的postProcessAfterInitialization
14.执行InstantiationAwareBeanPostProcessorAdapter的后置方法,postProcessAfterInitialization
15.Bean的使用
16.调用DiposibleBean的destory方法
17.调用bean指定的destory-method方法
具体实现:
package com.zsm.spring.beanfactory;import org.springframework.beans.BeansException;import org.springframework.beans.factory.*;/** * @author: zhangsiming * @Date: 2020/11/20 13:54 * @Description: spring bean 生命周期 * 实现BeanFactoryAware的bean 可直接访问spring ioc 被容器创建后,会拥有一个指向ioc容器的引用,利用该bean根据传入的参数动态获取被spring工厂加载的bean * BeanNameAware 获取容器中bean的名称 * InitializingBean 为bean提供初始化方法,InitializingBean 只包括afterPropertiesSet方法继承该接口的类,在初始化bean时都会执行该方法 * DisposableBean 销毁容器中的bean */public class Employee implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean { private String name; private int age; private String phone; private BeanFactory beanFactory; private String beanName; public Employee() { System.out.println("【构造器】调用employee的构造器实例化"); } public String getName() { return name; } public void setName(String name) { System.out.println("【注入属性】注入属性name"); this.name = name; } public int getAge() { return age; } public void setAge(int age) { System.out.println("【注入属性】注入属性age"); this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { System.out.println("【注入属性】注入属性phone"); this.phone = phone; } @Override public String toString() { return "Employee{" + "name='" + name + '\'' + ", age=" + age + '}'; } /** * BeanFactoryAware 接口方法 * * @param beanFactory * @throws BeansException */ @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()"); this.beanFactory = beanFactory; } /** * BeanNameAware 接口方法 * * @param s */ @Override public void setBeanName(String s) { System.out.println("【BeanNameAware接口】调用BeanNameAware.setBeanName()"); this.beanName = s; } /** * DisposableBean 接口方法 * * @throws Exception */ @Override public void destroy() throws Exception { System.out.println("【DisposableBean接口】调用DisposableBean.destroy()"); } /** * InitializingBean 接口方法 * * @throws Exception */ @Override public void afterPropertiesSet() throws Exception { System.out.println("【InitializingBean接口】调用InitializingBean.afterPropertiesSet()"); } /** * 通过的init-method属性指定的初始化方法 */ public void myInit() { System.out.println("【init-method】调用的init-method属性指定的初始化方法"); } /** * 通过的destroy-method属性指定的初始化方法 */ public void myDestory() { System.out.println("【destroy-method】调用的destroy-method属性指定的初始化方法"); }}
package com.zsm.spring.beanfactory;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanDefinition;import org.springframework.beans.factory.config.BeanFactoryPostProcessor;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;/** * @author: zhangsiming * @Date: 2020/11/20 15:01 * @Description: 自定义 BeanFactory后置处理器 */public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { public MyBeanFactoryPostProcessor() { super(); System.out.println("BeanFactoryPostProcessor 实现类的构造器"); } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { System.out.println("BeanFactoryPostProcessor 调用 postProcessBeanFactory方法 "); BeanDefinition definition = configurableListableBeanFactory.getBeanDefinition("employee"); definition.getPropertyValues().addPropertyValue("name", "zsm"); }}
package com.zsm.spring.beanfactory;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;/** * @author: zhangsiming * @Date: 2020/11/20 14:21 * @Description: 自定义后置处理器 * BeanPostProcessor接口包括2个方法postProcessAfterInitialization * 和postProcessBeforeInitialization,这两个方法的第一个参数都是要处理的Bean对象, * 第二个参数都是Bean的name。返回值也都是要处理的Bean对象 */public class MyBeanPostProcessor implements BeanPostProcessor { public MyBeanPostProcessor() { super(); System.out.println("这是BeanPostProcessor实现类的构造器!!"); } @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { //对bean 进行初始化前执行 System.out.println("BeanPostProcessor接口方法postProcessBeforeInitialization对属性进行更改!"); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { //对bean 进行初始化后执行 System.out.println("BeanPostProcessor接口方法postProcessAfterInitialization对属性进行更改!"); return bean; }}
package com.zsm.spring.beanfactory;import org.springframework.beans.BeansException;import org.springframework.beans.PropertyValues;import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;import java.beans.PropertyDescriptor;/** * @author: zhangsiming * @Date: 2020/11/20 14:29 * @Description: 自定义一InstantiationAwareBeanPostProcessor * InstantiationAwareBeanPostProcessor 接口本质是BeanPostProcessor的子接口, * 一般我们继承Spring为其提供的适配器类InstantiationAwareBeanPostProcessor Adapter来使用它 * 这个有3个方法,其中第二个方法postProcessAfterInitialization就是重写了BeanPostProcessor的方法。 * 第三个方法postProcessPropertyValues用来操作属性,返回值也应该是PropertyValues对象。 */public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter { public MyInstantiationAwareBeanPostProcessor() { System.out.println("这是InstantiationAwareBeanPostProcessor 实现类的构造方法"); } /** * 接口方法,实例化bean之前调用 * * @param beanClass * @param beanName * @return * @throws BeansException */ @Override public Object postProcessBeforeInstantiation(Class> beanClass, String beanName) throws BeansException { System.out.println("InstantiationAwareBeanPostProcessor 调用postProcessBeforeInstantiation方法 "); return null; } /** * 接口方法,实例化Bean之后调用 * * @param bean * @param beanName * @return * @throws BeansException */ @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("InstantiationAwareBeanPostProcessor 调用 postProcessAfterInitialization 方法 "); return bean; } /** * 接口方法 设置某个属性是调用 * * @param pvs * @param pds * @param bean * @param beanName * @return * @throws BeansException */ @Override public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { System.out.println("InstantiationAwareBeanPostProcessor 调用 postProcessPropertyValues 方法 "); return pvs; }}
package com.zsm.spring.beanfactory;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;/** * @author: zhangsiming * @Date: 2020/11/20 15:09 * @Description: 配置类 */@Configuration@ComponentScan(value = "com.zsm.spring.beanfactory")public class SpringConfiguration { /** * 用于配置公共配置 */ /** * 配置Bean的后置处理器 * * @return */ @Bean("beanPostProcessor") public MyBeanPostProcessor myBeanPostProcessor() { return new MyBeanPostProcessor(); } /** * 配置instantiationAwareBeanPostProcessor * * @return */ @Bean("instantiationAwareBeanPostProcessor") public MyInstantiationAwareBeanPostProcessor myInstantiationAwareBeanPostProcessor() { return new MyInstantiationAwareBeanPostProcessor(); } /** * 配置BeanFactory的后置处理器 * * @return */ @Bean("beanFactoryPostProcessor") public MyBeanFactoryPostProcessor myBeanFactoryPostProcessor() { return new MyBeanFactoryPostProcessor(); } @Bean("employee") public Employee employee() { return new Employee(); } @Bean(initMethod = "myInit", destroyMethod = "myDestory") public Employee employee1() { return new Employee(); }}
package com.zsm.spring.beanfactory;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * @author: zhangsiming * @Date: 2020/11/20 15:16 * @Description: 测试bean的生命周期 */public class TestTheLifeCycleBean { public static void main(String[] args) { System.out.println("----------【初始化容器】----------"); AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class); System.out.println("----------【初始化容器成功】----------"); Employee employee = applicationContext.getBean("employee", Employee.class); System.out.println(employee); System.out.println("----------【销毁容器】----------"); applicationContext.close(); }}
执行结果
16:57:51.655 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Bean factory for org.springframework.context.annotation.AnnotationConfigApplicationContext@b065c63: org.springframework.beans.factory.support.DefaultListableBeanFactory@3a5ed7a6: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,springConfiguration]; root of factory hierarchy16:57:51.667 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'16:57:51.667 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'16:57:51.685 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references16:57:51.687 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'16:57:51.706 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning16:57:51.711 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Resolved classpath location [com/zsm/spring/beanfactory/] to resources [URL [file:/D:/project/Study/spring-boot-dubbo/spring-demo/target/classes/com/zsm/spring/beanfactory/]]16:57:51.712 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Looking for matching resources in directory tree [D:\project\Study\spring-boot-dubbo\spring-demo\target\classes\com\zsm\spring\beanfactory]16:57:51.712 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Searching directory [D:\project\Study\spring-boot-dubbo\spring-demo\target\classes\com\zsm\spring\beanfactory] for files matching pattern [D:/project/Study/spring-boot-dubbo/spring-demo/target/classes/com/zsm/spring/beanfactory/**/*.class]16:57:51.717 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Resolved location pattern [classpath*:com/zsm/spring/beanfactory/**/*.class] to resources [file [D:\project\Study\spring-boot-dubbo\spring-demo\target\classes\com\zsm\spring\beanfactory\Employee.class], file [D:\project\Study\spring-boot-dubbo\spring-demo\target\classes\com\zsm\spring\beanfactory\MyBeanFactoryPostProcessor.class], file [D:\project\Study\spring-boot-dubbo\spring-demo\target\classes\com\zsm\spring\beanfactory\MyBeanPostProcessor.class], file [D:\project\Study\spring-boot-dubbo\spring-demo\target\classes\com\zsm\spring\beanfactory\MyInstantiationAwareBeanPostProcessor.class], file [D:\project\Study\spring-boot-dubbo\spring-demo\target\classes\com\zsm\spring\beanfactory\SpringConfiguration.class], file [D:\project\Study\spring-boot-dubbo\spring-demo\target\classes\com\zsm\spring\beanfactory\TestTheLifeCycleBean.class]]16:57:51.756 [main] DEBUG org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader - Registering bean definition for @Bean method com.zsm.spring.beanfactory.SpringConfiguration.beanPostProcessor()16:57:51.756 [main] DEBUG org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader - Registering bean definition for @Bean method com.zsm.spring.beanfactory.SpringConfiguration.instantiationAwareBeanPostProcessor()16:57:51.757 [main] DEBUG org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader - Registering bean definition for @Bean method com.zsm.spring.beanfactory.SpringConfiguration.beanFactoryPostProcessor()16:57:51.758 [main] DEBUG org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader - Registering bean definition for @Bean method com.zsm.spring.beanfactory.SpringConfiguration.employee()16:57:51.758 [main] DEBUG org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader - Registering bean definition for @Bean method com.zsm.spring.beanfactory.SpringConfiguration.employee1()16:57:51.831 [main] DEBUG org.springframework.context.annotation.ConfigurationClassEnhancer - Successfully enhanced com.zsm.spring.beanfactory.SpringConfiguration; enhanced class name is: com.zsm.spring.beanfactory.SpringConfiguration$$EnhancerBySpringCGLIB$$a1ed674016:57:51.832 [main] DEBUG org.springframework.context.annotation.ConfigurationClassPostProcessor - Replacing bean definition 'springConfiguration' existing class 'com.zsm.spring.beanfactory.SpringConfiguration' with enhanced class 'com.zsm.spring.beanfactory.SpringConfiguration$$EnhancerBySpringCGLIB$$a1ed6740'16:57:51.832 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'beanFactoryPostProcessor'16:57:51.832 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'beanFactoryPostProcessor'16:57:51.834 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'springConfiguration'16:57:51.834 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'springConfiguration'16:57:51.834 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'springConfiguration' to allow for resolving potential circular references16:57:51.866 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'springConfiguration'16:57:51.872 [main] WARN org.springframework.context.annotation.ConfigurationClassEnhancer - @Bean method SpringConfiguration.myBeanFactoryPostProcessor is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details.BeanFactoryPostProcessor 实现类的构造器16:57:51.891 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'beanFactoryPostProcessor' to allow for resolving potential circular references16:57:51.893 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'beanFactoryPostProcessor'BeanFactoryPostProcessor 调用 postProcessBeanFactory方法 16:57:51.898 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'16:57:51.898 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'16:57:51.899 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' to allow for resolving potential circular references16:57:51.905 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'16:57:51.906 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'16:57:51.906 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'16:57:51.906 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' to allow for resolving potential circular references16:57:51.908 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'16:57:51.908 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'16:57:51.908 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'16:57:51.911 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' to allow for resolving potential circular references16:57:51.916 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'16:57:51.916 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'beanPostProcessor'16:57:51.916 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'beanPostProcessor'16:57:51.916 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'springConfiguration'这是BeanPostProcessor实现类的构造器!!16:57:51.923 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'beanPostProcessor' to allow for resolving potential circular references16:57:51.925 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'beanPostProcessor'16:57:51.925 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'instantiationAwareBeanPostProcessor'16:57:51.926 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'instantiationAwareBeanPostProcessor'16:57:51.926 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'springConfiguration'这是InstantiationAwareBeanPostProcessor 实现类的构造方法16:57:51.928 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'instantiationAwareBeanPostProcessor' to allow for resolving potential circular references16:57:51.930 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'instantiationAwareBeanPostProcessor'16:57:51.932 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@161479c6]16:57:51.934 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@56528192]16:57:51.935 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3a5ed7a6: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,springConfiguration,beanPostProcessor,instantiationAwareBeanPostProcessor,beanFactoryPostProcessor,employee,employee1]; root of factory hierarchy16:57:51.935 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'16:57:51.935 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'16:57:51.938 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'16:57:51.938 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'16:57:51.939 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'16:57:51.939 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'InstantiationAwareBeanPostProcessor 调用postProcessBeforeInstantiation方法 16:57:51.941 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.event.internalEventListenerProcessor' to allow for resolving potential circular referencesInstantiationAwareBeanPostProcessor 调用 postProcessPropertyValues 方法 BeanPostProcessor接口方法postProcessBeforeInitialization对属性进行更改!BeanPostProcessor接口方法postProcessAfterInitialization对属性进行更改!InstantiationAwareBeanPostProcessor 调用 postProcessAfterInitialization 方法 16:57:51.945 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'16:57:51.946 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'16:57:51.946 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'InstantiationAwareBeanPostProcessor 调用postProcessBeforeInstantiation方法 16:57:51.946 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.event.internalEventListenerFactory' to allow for resolving potential circular referencesInstantiationAwareBeanPostProcessor 调用 postProcessPropertyValues 方法 BeanPostProcessor接口方法postProcessBeforeInitialization对属性进行更改!BeanPostProcessor接口方法postProcessAfterInitialization对属性进行更改!InstantiationAwareBeanPostProcessor 调用 postProcessAfterInitialization 方法 16:57:51.949 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'16:57:51.949 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'springConfiguration'16:57:51.949 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'beanPostProcessor'16:57:51.949 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'instantiationAwareBeanPostProcessor'16:57:51.949 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'beanFactoryPostProcessor'16:57:51.950 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'employee'16:57:51.950 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'employee'InstantiationAwareBeanPostProcessor 调用postProcessBeforeInstantiation方法 16:57:51.950 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'springConfiguration'【构造器】调用employee的构造器实例化16:57:51.951 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'employee' to allow for resolving potential circular referencesInstantiationAwareBeanPostProcessor 调用 postProcessPropertyValues 方法 【注入属性】注入属性name【BeanNameAware接口】调用BeanNameAware.setBeanName()【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()BeanPostProcessor接口方法postProcessBeforeInitialization对属性进行更改!16:57:51.980 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'employee'【InitializingBean接口】调用InitializingBean.afterPropertiesSet()BeanPostProcessor接口方法postProcessAfterInitialization对属性进行更改!InstantiationAwareBeanPostProcessor 调用 postProcessAfterInitialization 方法 16:57:51.981 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'employee'16:57:51.982 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'employee1'16:57:51.982 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'employee1'InstantiationAwareBeanPostProcessor 调用postProcessBeforeInstantiation方法 16:57:51.983 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'springConfiguration'【构造器】调用employee的构造器实例化16:57:51.984 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'employee1' to allow for resolving potential circular referencesInstantiationAwareBeanPostProcessor 调用 postProcessPropertyValues 方法 【BeanNameAware接口】调用BeanNameAware.setBeanName()【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()BeanPostProcessor接口方法postProcessBeforeInitialization对属性进行更改!16:57:51.984 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'employee1'【InitializingBean接口】调用InitializingBean.afterPropertiesSet()16:57:51.984 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking init method 'myInit' on bean with name 'employee1'【init-method】调用的init-method属性指定的初始化方法BeanPostProcessor接口方法postProcessAfterInitialization对属性进行更改!InstantiationAwareBeanPostProcessor 调用 postProcessAfterInitialization 方法 16:57:51.984 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'employee1'16:57:51.985 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'16:57:52.010 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@3d74bf60]16:57:52.011 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'16:57:52.015 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source----------【初始化容器成功】----------16:57:52.018 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'employee'Employee{name='zsm', age=0}----------【销毁容器】----------16:57:52.019 [main] INFO org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@b065c63: startup date [Fri Nov 20 16:57:51 CST 2020]; root of context hierarchy16:57:52.019 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'16:57:52.020 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3a5ed7a6: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,springConfiguration,beanPostProcessor,instantiationAwareBeanPostProcessor,beanFactoryPostProcessor,employee,employee1]; root of factory hierarchy16:57:52.020 [main] DEBUG org.springframework.beans.factory.support.DisposableBeanAdapter - Invoking destroy() on bean with name 'employee1'【DisposableBean接口】调用DisposableBean.destroy()16:57:52.020 [main] DEBUG org.springframework.beans.factory.support.DisposableBeanAdapter - Invoking destroy method 'myDestory' on bean with name 'employee1'【destroy-method】调用的destroy-method属性指定的初始化方法16:57:52.021 [main] DEBUG org.springframework.beans.factory.support.DisposableBeanAdapter - Invoking destroy() on bean with name 'employee'【DisposableBean接口】调用DisposableBean.destroy()Process finished with exit code 0