spring 启动器的实现

spring 的starter启动器能实现默认配置
starter的目录如下:
在这里插入图片描述

resources 目录中META-INF 中的 spring.factories 是启动器的关键。
其中的内容如下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alex.MyStarterAutoConfiguration

spring 在启动加载时,会读取该配置中的 EnableAutoConfiguration 下所有类并加载,如会加载com.alex.MyStarterAutoConfiguration;
该类的代码如下:

package com.alex;

import com.mysql.cj.jdbc.Driver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author LiXin
 * @date 2022-03-13 15:39
 */
@Configuration
@EnableConfigurationProperties(MyStarterProperties.class)
@ConditionalOnClass(MyStarter.class)
public class MyStarterAutoConfiguration {

    @Autowired
    private MyStarterProperties myStarterProperties;

    @Bean
    @ConditionalOnMissingBean(MyStarter.class)
    @ConditionalOnClass(Driver.class)
    public MyStarter myStarter() {
        return new MyStarter(myStarterProperties.getName());
    }
}

xxxAutoConfiguration 被spring识别后,会将所有的 @Bean 加载进 spring 容器,如这里的MyStarter。
注意这里的 @ConditionalOnClass(Driver.class) 意思是只有当 starter 的使用方引入了 Mysql 的驱动后,才会生成 MyStarter 的 bean 。
为了避免使用方引入本 starter 时就引用了 Mysql 的驱动,在 maven 依赖中 scope 是provided。
如下:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>mystarter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.2.6.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <!-- 不会被编译进jar包中 -->
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>```

```java
package com.alex;

/**
 * @author LiXin
 * @date 2022-03-13 15:40
 */
public class MyStarter {
    private String name;

    public MyStarter(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

配置文件就不多说了:

package com.alex;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @author LiXin
 * @date 2022-03-13 15:40
 */
@ConfigurationProperties(prefix = "com.alex")
public class MyStarterProperties {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

在 starter 中的默认配置放在 starter 的 .yaml 文件中。如果客户端(即starter的使用方)需要使用自定义的配置,只需在自己的 .yaml 中覆盖该配置即可。

当客户端引用了某个启动器,而想要排除掉某些 xxxAutoConfiguration 时,写法如下:
在这里插入图片描述

注意这里被exclude 的类只能是 spring.factories 中 org.springframework.boot.autoconfigure.EnableAutoConfiguration 配置的类,不然会报错:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值