之前的文章中进行过springboot的一些使用,再说一说一些加强的内容.

SpringBoot启动原理说明

我们都知道springboot十分强大,可以实现零配置/少配置运行,以及开箱即用的特性,那么他是怎么做到的呢?

pom.xml

当我们创建一个springboot项目,并只在创建时导入spring web依赖时可以看到pom.xml中有什么配置:

<?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>

    <groupId>com.jt</groupId>
    <artifactId>springboot_demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_demo1</name>
    <description>入门案例</description>

    <!--parent标签作用: 定义了SpringBoot中所有关联项目的版本号信息.-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <properties>
        <java.version>1.8</java.version>
        <!--项目打包时,跳过测试类打包-->
        <skipTests>true</skipTests>
    </properties>

    <!--开箱即用:SpringBoot项目只需要引入少量的jar包及配置,即可拥有其功能.
        spring-boot-starter 拥有开箱即用的能力.
        maven项目中依赖具有传递性.
            A 依赖  B  依赖 C项目   导入A bc会自动依赖
    -->
    <dependencies>
        <!--直接依赖web springMVC配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <!--springBoot-start SpringBoot启动项  -->
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--SpringBoot重构了测试方式 可以在测试类中 直接引入依赖对象-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <!--在项目打包部署时生效,如果不添加build,则程序发布时不然会报
        项目中没有main方法.
    -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

pom.xml文件内标签的含义在上述代码中有所注释.

开箱即用

开箱即用:指项目中只需要注入少量的jar包及配置,就可拥有其功能.

就是上述的pom.xml中的spring-starter启动项拥有开箱即用的能力.

image
上图是开箱即用特性的一个梳理,接下来说一下:
首先启动类会执行

SpringApplication.run(SpringbootDemo01Application.class, args);

run方法会加载@SpringBootApplication注解:

  1. 有元注解(修饰注解的注解)
@Target(ElementType.TYPE)//注解对类有效
@Retention(RetentionPolicy.RUNTIME)//在运行期有效
@Documented//动态生成文档信息
@Inherited//可以被继承

2.加载对象,但要排除的过滤器

@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
        

3.springboot配置类

@SpringBootConfiguration又由@Configuration配置类以及元注解修饰

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {}

意味着主启动类本身就是一个超大的配置文件,可以去加载其他@Configuration注解描述的类,当启动类加载时,其他类都会加载.

4.完成开箱即用配置

@EnableAutoConfiguration由元注解以及@AutoConfigurationPackage和@Import(AutoConfigurationImportSelector.class)修饰

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {}

@AutoConfigurationPackage--自动按照包扫描的方式实例化对象,之后所有的配置都需要在启动类所在包以及子孙包中进行定义.

@Import(AutoConfigurationImportSelector.class)--当程序启动时,根据SpringBoot中的Seletor选择器,去检查pom.xml文件中是否添加了启动项的包.


迈克丝
85 声望5 粉丝

一步一步学技术,踏踏实实涨经验,兴趣广泛,广交好友,希望大家多多指正/批评.