Bean的生命周期
ApplicationContext
Central interface to provide configuration for an application.
* This is read-only while the application is running, but may be
* reloaded if the implementation supports this.
*
* <p>An ApplicationContext provides:
* <ul>
* <li>Bean factory methods for accessing application components.//bean工厂
* Inherited from {@link org.springframework.beans.factory.ListableBeanFactory}.
* <li>The ability to load file resources in a generic fashion.//加载资源
* Inherited from the {@link org.springframework.core.io.ResourceLoader} interface.
* <li>The ability to publish events to registered listeners.//发布事件
* Inherited from the {@link ApplicationEventPublisher} interface.
* <li>The ability to resolve messages, supporting internationalization.//处理信息,支持国际化
* Inherited from the {@link MessageSource} interface.
* <li>Inheritance from a parent context. Definitions in a descendant context
* will always take priority. This means, for example, that a single parent
* context can be used by an entire web application, while each servlet has
* its own child context that is independent of that of any other servlet.
//从父级上下文中继承。后代上下文中的定义将始终具有优先权。
这意味着,例如,一个单一的父级上下文可以被整个Web应用所使用,而每个Servlet都有 而每个servlet都有自己的子上下文,它独立于任何其他servlet的上下文。
* </ul>
*
* <p>In addition to standard {@link org.springframework.beans.factory.BeanFactory}
* lifecycle capabilities, ApplicationContext implementations detect and invoke
* {@link ApplicationContextAware} beans as well as {@link ResourceLoaderAware},
* {@link ApplicationEventPublisherAware} and {@link MessageSourceAware} beans.//Aware回调
创建BeanFactory,初始化BeanFactory,执行BeanFactory后置处理器
BeanFactory
**Condition
* The root interface for accessing a Spring bean container.//访问Spring容器的根接口
*
* <p>This is the basic client view of a bean container;
* further interfaces such as {@link ListableBeanFactory} and
* {@link org.springframework.beans.factory.config.ConfigurableBeanFactory}
* are available for specific purposes.
*
* <p>This interface is implemented by objects that hold a number of bean definitions,
* each uniquely identified by a String name. Depending on the bean definition,
* the factory will return either an independent instance of a contained object
* (the Prototype design pattern), or a single shared instance (a superior
* alternative to the Singleton design pattern, in which the instance is a
* singleton in the scope of the factory). Which type of instance will be returned
* depends on the bean factory configuration: the API is the same. Since Spring
* 2.0, further scopes are available depending on the concrete application
* context (e.g. "request" and "session" scopes in a web environment)//持有若干bean定义的对象,每个Bean定义由一个String名称唯一标识,Bean定义不同,工厂会返回一个独立实例或者
单一的共享实例;APi是一样的,返回什么类型取决于bean工厂的配置
*
* <p>The point of this approach is that the BeanFactory is a central registry
* of application components, and centralizes configuration of application
* components (no more do individual objects need to read properties files,
* for example). See chapters 4 and 11 of "Expert One-on-One J2EE Design and
* Development" for a discussion of the benefits of this approach.//beanFactory中央注册处,集中配置应用程序组件
*
* <p>Note that it is generally better to rely on Dependency Injection
* ("push" configuration) to configure application objects through setters
* or constructors, rather than use any form of "pull" configuration like a
* BeanFactory lookup. Spring's Dependency Injection functionality is
* implemented using this BeanFactory interface and its subinterfaces.//请注意,一般来说,依靠依赖注入("推 "式配置)来通过设置器或构造器配置应用对象,
而不是使用任何形式的 "拉 "式配置,如BeanFactory查找。Spring的依赖性注入功能是 使用这个BeanFactory接口及其子接口来实现。
*
* <p>Normally a BeanFactory will load bean definitions stored in a configuration
* source (such as an XML document), and use the {@code org.springframework.beans}
* package to configure the beans. However, an implementation could simply return
* Java objects it creates as necessary directly in Java code. There are no
* constraints on how the definitions could be stored: LDAP, RDBMS, XML,
* properties file, etc. Implementations are encouraged to support references
* amongst beans (Dependency Injection).//通常情况下,BeanFactory会加载存储在配置源(如XML文档)中的beandefinition,并使用{@code org.springframework.beans}包来配置bean。然而,一个实现可以简单地返回它在Java代码中直接创建的必要的Java对象。
对于定义的存储方式没有任何限制: LDAP、RDBMS、XML、属性文件等。我们鼓励实施者支持引用 bean之间的引用(依赖性注入)
*
* <p>In contrast to the methods in {@link ListableBeanFactory}, all of the
* operations in this interface will also check parent factories if this is a
* {@link HierarchicalBeanFactory}. If a bean is not found in this factory instance,
* the immediate parent factory will be asked. Beans in this factory instance
* are supposed to override beans of the same name in any parent factory.
与{@link ListableBeanFactory}中的方法不同,如果这是一个{@link HierarchicalBeanFactory},这个接口中的所有操作也将检查父工厂。
如果在这个工厂实例中没有找到Bean,那么就会询问直接的父工厂。这个工厂实例中的Bean应该覆盖任何父工厂中的同名Bean。
*
* <p>Bean factory implementations should support the standard bean lifecycle interfaces
* as far as possible. The full set of initialization methods and their standard order is:
* <ol>//Aware的一些设置
* <li>BeanNameAware's {@code setBeanName}
* <li>BeanClassLoaderAware's {@code setBeanClassLoader}
* <li>BeanFactoryAware's {@code setBeanFactory}
* <li>EnvironmentAware's {@code setEnvironment}
* <li>EmbeddedValueResolverAware's {@code setEmbeddedValueResolver}
* <li>ResourceLoaderAware's {@code setResourceLoader}
* (only applicable when running in an application context)
* <li>ApplicationEventPublisherAware's {@code setApplicationEventPublisher}
* (only applicable when running in an application context)
* <li>MessageSourceAware's {@code setMessageSource}
* (only applicable when running in an application context)
* <li>ApplicationContextAware's {@code setApplicationContext}
* (only applicable when running in an application context)
* <li>ServletContextAware's {@code setServletContext}
* (only applicable when running in a web application context)
* <li>{@code postProcessBeforeInitialization} methods of BeanPostProcessors
* <li>InitializingBean's {@code afterPropertiesSet}
* <li>a custom {@code init-method} definition
* <li>{@code postProcessAfterInitialization} methods of BeanPostProcessors
* </ol>
*
* <p>On shutdown of a bean factory, the following lifecycle methods apply:
* <ol>
* <li>{@code postProcessBeforeDestruction} methods of DestructionAwareBeanPostProcessors
* <li>DisposableBean's {@code destroy}
* <li>a custom {@code destroy-method} definition
* </ol>
条件在Bean-definition即将被注册之前被检查,并且可以根据当时可以确定的任何标准自由地否决注册。
条件必须遵循与{@link BeanFactoryPostProcessor}相同的限制,并注意不要与Bean实例交互。如果要对与{@code @Configuration} Bean交互的条件进行更精细的控制,可以考虑实现 {@link ConfigurationCondition}接口。
1.Spring启动,扫描包路径,获得Beandefinition的集合(Resource对象,获取MetaDataReader,然后得到对象信息,将ScannerGenericBeanDefinition加入结果集)
MetaDataReader(Facade设计模式,ASM技术)
2.合并BeanDifinition,因为父类的属性需要继承
3.加载类,AbstractAutowireCapableBeanFactory去创建bean对象
4.实例化前,InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation
Subinterface of {@link BeanPostProcessor} that adds a before-instantiation callback,
* and a callback after instantiation but before explicit properties are set or
* autowiring occurs.
*
* <p>Typically used to suppress default instantiation for specific target beans,
* for example to create proxies with special TargetSources (pooling targets,
* lazily initializing targets, etc), or to implement additional injection strategies
* such as field injection.
*
* <p><b>NOTE:</b> This interface is a special purpose interface, mainly for
* internal use within the framework. It is recommended to implement the plain
* {@link BeanPostProcessor} interface as far as possible
5.实例化
Supplier创建对象(有就用)
/**
* Return a callback for creating an instance of the bean, if any.
* @since 5.0
*/
工厂方法创建对象(有就用)
推断构造方法(LookUp注解)lookup注解,方法注入,CGLib动态代理的方式
这样的查找方法可以有默认的(stub)实现,它们将被容器简单地替换,或者它们可以被声明为抽象的--以便容器在运行时填充它们。在这两种情况下,容器将通过 CGLIB 生成该方法的包含类的运行时子类,这就是为什么这种查找方法只能在容器通过常规构造函数实例化的 Bean 上工作:也就是说,查找方法不能被替换到从工厂方法返回的 Bean 上,因为我们不能为它们动态地提供子类。
* 当在某些情况下可能需要一个具体的类时,考虑提供存根
* 实现你的查找方法。而且请记住,查找方法
* 不会对配置类中{@code @Bean}方法返回的bean起作用;
* 你必须使用{@code @Inject Provider<TargetBean>}或类似的方法来代替。
6.BeanDefinition后置处理
对BeanDefinition加工,MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition()
7.实例化后
对实例化的对象进行处理(没怎么用)
8.自动注入
9.处理属性
处理注解,通过InstantiationAwareBeanPostProcessor.postProcessProperties扩展点来实现的
10.执行Aware
传递一些值给bean对象
11.初始化前
BeanPostProcessor.postProcessBeforeInitialization
12.初始化
查看Bean是否实现了InitializingBean接口,实现了就调用afterPropertiesSet()方法
执行BeanDefinition指定的初始化方法
13.初始化后
BeanPostProcessor.postProcessAfterInitialization(可以进行一些AOP的操作)
BeanPostProcessor
* Factory hook that allows for custom modification of new bean instances —
* for example, checking for marker interfaces or wrapping beans with proxies.
*
* <p>Typically, post-processors that populate beans via marker interfaces
* or the like will implement {@link #postProcessBeforeInitialization},
* while post-processors that wrap beans with proxies will normally
* implement {@link #postProcessAfterInitialization}.
*
* <h3>Registration</h3>
* <p>An {@code ApplicationContext} can autodetect {@code BeanPostProcessor} beans
* in its bean definitions and apply those post-processors to any beans subsequently
* created. A plain {@code BeanFactory} allows for programmatic registration of
* post-processors, applying them to all beans created through the bean factory.
*
标签:code,复习,Spring,Bean,factory,bean,源码,link,beans From: https://www.cnblogs.com/WZXwzx/p/17282237.html