关于springboot:SpringBoot加强01

8次阅读

共计 3071 个字符,预计需要花费 8 分钟才能阅读完成。

之前的文章中进行过 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 启动项领有开箱即用的能力.


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

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

正文完
 0