iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot详细讲解如何创建及刷新Spring容器bean
  • 291
分享到

SpringBoot详细讲解如何创建及刷新Spring容器bean

2024-04-02 19:04:59 291人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录一、前期准备1.1 创建工程1.2 创建Controller二、探究过程2.1 启动类2.2 springApplication2.3 ApplicationContextFac

  • 参考视频:https://www.bilibili.com/video/BV1Bq4y1Q7GZ?p=6
  • 通过视频的学习和自身的理解整理出的笔记

一、前期准备

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方法。

到此这篇关于SpringBoot详细讲解如何创建及刷新Spring容器bean的文章就介绍到这了,更多相关SpringBoot Spring容器bean内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringBoot详细讲解如何创建及刷新Spring容器bean

本文链接: https://www.lsjlt.com/news/150961.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作