SpringBoot增强01

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

SpringBoot启动原理说明

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

pom.xml

当咱们建立一个springboot项目,并只在建立时导入spring web依赖时能够看到pom.xml中有什么配置:spring

<?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文件内标签的含义在上述代码中有所注释.apache

开箱即用

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

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

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

SpringApplication.run(SpringbootDemo01Application.class, args);

run方法会加载@SpringBootApplication注解:测试

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

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

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

3.springboot配置类spa

@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文件中是否添加了启动项的包.