首页 > 编程语言 >Spring源码复习

Spring源码复习

时间:2023-04-03 11:44:55浏览次数:49  
标签:code 复习 Spring Bean factory bean 源码 link beans

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

**
* 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>
 Condition  

条件在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 &mdash;
* 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

相关文章

  • Spring Initailizr(项目初始化向导)
    本地创建官网创建版在Spring官网https://start.spring.io/中选择此时这个项目以压缩包形式下载到本地文件中,然后解压,导入IDEA中阿里start创建如果国外的网址不能通过,将网址更换为http://start.aliyun.com更换版本号在pom.xml中更换maven依赖......
  • 【Spring】注解器
    applicationContext.xml1<?xmlversion="1.0"encoding="UTF-8"?>2<beansxmlns="http://www.springframework.org/schema/beans"3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"4xmln......
  • day01-SpringCloud基本介绍
    SpringCloud基本介绍SpringCloud官方文档1.提出问题先思考一个问题,没有微服务技术,是不是程序员就不能开发大型项目?是可以的,对大型项目进行模块划分,对各个模块进行实现。但模块之间更多地是以API调用完成,耦合度较高,不利于拓展和维护(在没有微服务技术时,很多大型项目就已经使......
  • C#复习笔记-委托
       委托是一种引用类型,委托定义了了一类可以被委托实例调用的方法。它定义了方法的返回值类型和参数类型。定义了一个名为FeedBack的委托,返回一个int类型的值,带有一个int类型的参数。可以将任何类型或者结构中与委托类型匹配的方法传递给委托,可以是静态方法也可以是实例方法......
  • SpringCloud之sleuth
    在大型分布式系统中,一次调用可能要经过很多不同的系统,调用很多服务。每个服务之间的调用会越来越复杂。会引入以下问题:如何快速发现问题?如何判断故障影响范围?如何梳理服务依赖以及依赖的合理性?如何分析链路性能问题以及实时容量规划? 为了快速定位问题及时解决问题,引入了......
  • 新概念2册L75笔记(复习一般过去时&系动词:变化)
    L75SOS单词理解语法理解一般过去时功能:发生在过去的事情;礼貌委婉。关键词:过去具体时间(yesterday/ago/last…)课文理解......
  • SpringBoot集成Activiti7-单独配置数据源
    框架:SpringBoot+Mybatis+Activiti7思路:单独给mybatis和activiti配置datasourceMybati配置单数据源方法单数据源只需要在yml中配置url:jdbc:mysql://localhost:3306/localtest?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatemen......
  • C 语言程序设计复习
    第一章程序设计和C语言计算机程序一组计算机能够识别和执行的指令计算机语言机器语言计算机只能识别由0和1组成的指令能够别计算机识别和接受的二进制代码成为机器指令机器指令的集合就是机器语言符号语言(汇编语言)计算机不能直接识别和执行,需要汇编程序将其转换为机......
  • SpringCloud之zuul
    后台的微服务和客户端之间都有一个类似于酒店的前台,叫做网关。网关一般有以下作用:1、统一登录认证,由网关来进行身份认证,调用每个微服务时就不用各自认证了。2、解决跨越问题,微服务一般是部署在内网中。客户端调用时存在跨域。3、动态路由和负载均衡,根据请求路径动态分发到不同......
  • 【转】【Revit】revit二次开发——基于teigha读取CAD几何与文字(提供源码、引用包与异
    1.下载64位Teigha完整包:链接:https://pan.baidu.com/s/1KXW54YlkkWJZNQYhbc99kw提取码:05052.VS平台架构选择64位,.net版本4.0以上:  3.dll必须手动指定引用:(重点)staticstringdll=@"D:\Desktop\Rvt\bin\Debug\TD_Mgd.dll";//引用位置Assemblya=Assembly.UnsafeL......