SpringBoot自定义Starter与自动配置实现方法详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringBoot自定义Starter与自动配置实现方法详解

你家宝宝   2023-03-20 我要评论

前言

前段时间,SpringBoot 出 3.x 版本了。听说把自动配置给刀了!!(3.x版本不再使用 spring.factories做自动配置)

但是这个在真正开始说要弃用,是在 2.7版本。只是向下兼容了 spring.factories 的配置方式。

也就是说两种写法共存,如下图:

META-INF 目录下增加了 spring 目录,其中有文件 org.springframework.boot.autoconfigure.AutoConfiguration.imports

文件内容是配置类的完整类名。

比如我当前使用的配置内容是:

org.feng.config.AppInfoConfiguration

而 spring.factories 中的配置内容和之前一致:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.feng.config.AppInfoConfiguration

这一点其实也可以从 Spring 的源码包中看到:

本次练习的代码仓库

https://gitee.com/fengsoshuai/custom-springboot-starter-demo.git

代码简要说明

模块说明
customer-starter自定义的 starter,并提供配置、示例接口&实现类
test测试自定义starter,引入自定义starter的依赖,并定义了启动类,控制器类

custom-springboot-starter-demo 的pom文件

主要定义了 SpringBoot的版本。

<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>custom-springboot-starter-demo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>custom-springboot-starter-demo</name>
  <url>http://maven.apache.org</url>
  <modules>
    <module>custom-starter</module>
    <module>test</module>
  </modules>
  <properties>
    <java.version>11</java.version>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <springboot.dependencies.version>2.7.8</springboot.dependencies.version>
  </properties>
  <dependencyManagement>
    <dependencies>
      <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${springboot.dependencies.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.1</version>
          <configuration>
            <source>11</source>
            <target>11</target>
            <encoding>UTF-8</encoding>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

customer-starter 的pom文件

<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>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>custom-springboot-starter-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>custom-starter</artifactId>
    <packaging>jar</packaging>
    <name>custom-starter</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
    <build>
        <!-- 将自定义starter中的默认配置文件也打包 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.yml</include>
                    <include>**/*.yaml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>

test 的pom文件

<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>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>custom-springboot-starter-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>test</artifactId>
    <packaging>jar</packaging>
    <name>test</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>custom-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

配置类

package org.feng.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
/**
 * 自动配置类:在META-INF中不做配置时,会抛出警告 <code>Application context not configured for this file</code>
 *
 * @version V1.0
 */
@AutoConfiguration
public class AppInfoConfiguration {
    @Value("${app.url.baiDu}")
    private String baiDuUrl;
    public String getBaiDuUrl() {
        return baiDuUrl;
    }
    public void setBaiDuUrl(String baiDuUrl) {
        this.baiDuUrl = baiDuUrl;
    }
    @Bean
    public AppUrl generateAppUrl() {
        AppUrl appUrl = new AppUrl();
        appUrl.setBaidu(baiDuUrl);
        return appUrl;
    }
}

配置信息

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们