欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring 底层源码解析(开篇)

程序员文章站 2022-03-05 22:05:49
...

本文基于Spring5注解版进行分析


做Java开发的程序员相信都用过Spring框架,也知道IOC,DI,AOP。

但是IOC是什么呢?它是怎么创建对象的呢?它又是怎么管理对象的呢?
DI又是怎么帮我们给对象的属性赋值的呢?会在什么时候进行赋值呢?
AOP是怎么实现的呢?AOP又是怎么工作的呢?

为什么要去了解Spring框架的底层原理呢?

只有在深入的了解到一个框架的底层原理,才方便我们去合理的用它。在我们写代码的时候,可以清晰的知道该怎么用框架提供的功能。



进入正题:在分析Spring底层源码的时候,应该要带着一些疑问,也就是知道Spring是什么,可以干什么,但是不知道怎么实现的。
例如:

  • Spring是在怎么进行扫包的。@ComponentScan()
  • @Configuration的作用有什么。
  • Spring是怎么帮我们创建对象的。
  • @Autowired是怎么注入的。
  • AOP的代理类是怎么创建的。
  • AOP的通知方法是怎么执行的。
  • @Transactional是怎么帮我们提交/回滚事务的。
  • @Enable xxx的启用注解,是怎么开启某个功能的。

Maven依赖:

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.10.RELEASE</version>
        </dependency>
    </dependencies>

启动Spring代码

public class StartSpring {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext
                = new AnnotationConfigApplicationContext(StartConfig.class);
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println("BeanName:" + name);
        }
    }
}

StartConfig代码截图,及项目截图
Spring 底层源码解析(开篇)
启动截图
Spring 底层源码解析(开篇)

因为StartConfig类上加了@ComponentScan(“com.liuqi”)所以会有userController,userDao,userService。
但是红框里面的并不是我项目的类,显然是Srping内部提供的,但是这些类的作用是什么呢?下面就开始进行分析


new AnnotationConfigApplicationContext(StartConfig.class)实例化做了什么工作

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(StartConfig.class);

AnnotationConfigApplicationContext类图:

Spring 底层源码解析(开篇)

  • BeanFactory:Bean工厂,也就是IOC容器的接口。
  • AnnotationConfigRegistry:@Configuration的注册接口。
  • BeanDefinitionRegistry:bean定义的注册中心的接口。
  • ApplicationEventPublisher:事件派发接口。

创建AnnotationConfigApplicationContext源码:

Spring 底层源码解析(开篇)

this() 实例化流程图:

流程图非常重要,请认真查看。画得也很清晰。步骤写得很明确
Spring 底层源码解析(开篇)

从以上流程看出,在GenericApplicationContext父类构造函数里面,创建了beanFactory (DefaultListableBeanFactory)

DefaultListableBeanFactory:是IOC容器的核心类,里面有很多Map属性,用于存放Spring创建的Bean。
类图:
Spring 底层源码解析(开篇)

  • DefaultSingletonBeanRegistry:单实例Bean的定义信息类。
  • FactoryBeanRegistrySupport:支持需要处理的单例注册的基类。
  • AbstractAutowireCapableBeanFactory:实现默认bean创建的抽象bean工厂超类。
  • DefaultListableBeanFactory:存放着所有的单实例Bean。

以上流程图在第十步的时候注册了Spring自带的Bean

this.reader = new AnnotatedBeanDefinitionReader(this);

回归到以上问题,Spring自动注册的Bean干什么用的。
Spring 底层源码解析(开篇)

  • org.springframework.context.annotation.internalConfigurationAnnotationProcessor
    实际是:ConfigurationAnnotationProcessor,它实现的了BeanFactoryPostProcessor,BeanDefinitionRegistryPostProcessor接口,说明是一个后置处理器。
    这个类就是用来解析@Configuration注解的配置类,@ComponentScan,
    @ComponentScans,@Import,@Bean等注解
    BeanDefinitionRegistryPostProcessor此接口用于注册Bean。
    注解版的Spring最为核心的类
    Spring 底层源码解析(开篇)
  • org.springframework.context.annotation.internalAutowiredAnnotationProcessor
    实际是:AutowiredAnnotationProcessor,实现的BeanPostProcessor,是一个后置处理器。
    对@Autowired,@Value,@Lookup注入支持。
    Spring 底层源码解析(开篇)
  • org.springframework.context.annotation.internalCommonAnnotationProcessor
    实际是:CommonAnnotationProcessor,它也是一个后置处理器。
    处理:@Resource,@WebServiceRef,@PostConstruct等注解,也就是javax提供的规范注解 JSR-250 supportSpring 底层源码解析(开篇)
  • org.springframework.context.event.internalEventListenerProcessor
    实际是:EventListenerMethodProcessor,也是后置处理器。
    标注了 @EventListener 的方法进行解析, 然后转换为一个 ApplicationListener。

Spring 底层源码解析(开篇)

  • org.springframework.context.event.internalEventListenerFactory
    实际是:DefaultEventListenerFactory,添加一个默认的EventListenerFactory实例,创建ApplicationListener。

总结:

  1. 在父类(GenericApplicationContext)中创建了BeanFactory(DefaultListableBeanFactory),这就是IOC的容器。
  2. DefaultListableBeanFactory添加了自动忽略接口BeanNameAware,BeanFactoryAware,BeanClassLoaderAware。
  3. 在初始化自己的时候,想向容器中注册了一些后置处理器。ConfigurationClassPostProcessor,AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,EventListenerMethodProcessor


以上分析了AnnotationConfigApplicationContext初始化父类及本类的过程,下篇文章再接着分析容器扫包,和创建Bean。

觉得对您有帮助,就点个赞呗。????
相关标签: spring java ioc