【Spring源码】SpringBoot中Spring容器bean的创建与刷新

44 篇文章 1 订阅
20 篇文章 0 订阅

前面看spring源码时可以发现refresh()方法十分重要。在这个方法中会加载beanDefinition,同时创建bean对象。那么在springboot中有没有使用这个refresh()方法呢?




一、前期准备

1.1 创建工程

创建springboot项目,springboot版本为2.5.0引入spring-boot-starter-web依赖,pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1.2 创建Controller

创建一个简单的Controller用于测试

@RestController
public class HelloController {
    
    public void helloController() {
        System.out.println("创建了");
    }
    
    @RequestMapping("hello")
    public String hello() {
        return "hello";
    }
}

二、探究过程

2.1 启动类

项目的运行只需要启动类中一行简单的代码,spring容器的创建就是通过SpringApplication.run(SpringbootApplication.class, args)这一行代码实现的。

在这里插入图片描述
其实就是调用了静态的run方法,传入了启动类的字节码对象。

2.2 SpringApplication

🔶 run()方法

又调用了另一个run方法。

public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
    return run(new Class[]{primarySource}, args);
}

🔶 run()方法

方法的返回值类型为ConfigurableApplicationContextApplicationContext就是Spring的容器。

public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
	return new SpringApplication(primarySources).run(args);
}

我们来看看ConfigurableApplicationContext 继承结构

在这里插入图片描述

ConfigurableApplicationContext接口继承了ApplicationContext接口

下面我们通过debug进入run()方法中看看
在这里插入图片描述
🔶 run()方法
在这里插入图片描述

🔶 createApplicationContext()方法

容器工厂传入webApplication的类型,这个类型为Servlet应用。

Springboot可以做响应式的Web开发,这时webApplication的类型的类型就不是Servlet。

在这里插入图片描述

2.3 ApplicationContextFactory

这里是一个lambda表达式的写法,根据webApplicationType的类型返回对应的容器对象。

在这里插入图片描述
通过继承关系图可以看出,它的确是ApplicationContext

在这里插入图片描述

2.4 SpringApplication

🔶 run()方法,容器刷新前

看完了createApplicationContext()的过程,我们再次回到run()方法中,此时context已经创建完成。

在这里插入图片描述
我们观察一下context中都有些什么:

在这里插入图片描述
其中包含了bean工厂,bean工厂里有beanDefinitionNames和beanDefinitionMap。(与之前看的spring源码一样)

不过这里都是spring容器内置的beanDefinition对象,没有我们自定义的helloController说明现在的容器还没有刷新

我们现在获取不到HelloController的bean对象,当我们能获取到这个对象时,就说明容器刷新了。

在这里插入图片描述

🔶 run()方法,容器刷新后

继续往下运行,我们发现这行代码执行了好久,根据方法名称也可以看出它的功能就是刷新容器。

在这里插入图片描述

刷新后我们成功的获取到了bean对象。

在这里插入图片描述

此时beanDefinitionMap中包含了138个对象,刷新之前只包含5个。我们可以在里面找到helloController(Hello的H变成了小写)

在这里插入图片描述

🔶 refreshContext()方法

下面我们看看refreshContext()方法,其中调用了refresh()方法。

在这里插入图片描述
🔶 refresh()方法

refreshContext()方法中refresh(context)传入了容器对象,在这里调用了这个容器对象的refresh()方法。

在这里插入图片描述

后面暂时不往下看了。

2.5 结论

在启动类中调用SpringApplication的run方法时会根据容器的类型创建不同的容器对象,并调用容器的refresh方法。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天边星宿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值