开发者

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

开发者 https://www.devze.com 2023-02-08 10:29 出处:网络 作者: 你家宝宝
目录前言本次练习的代码仓库代码简要说明custom-springboot-starter-demo 的pom文件customer-starter 的pom文件test 的pom文件配置类配置信息前言
目录
  • 前言
  • 本次练习的代码仓库
  • 代码简要说明
    • custom-springboot-starter-demo 的pom文件
    • customer-starter 的pom文件
    • test 的pom文件
    • 配置类
    • 配置信息

前言

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

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

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

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

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 的源码包中看到:

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

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

本次练习的代码仓库

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 -->
开发者_Go学习      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${springboot.dependencies.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  &phplt;/dependencyManagement>
  <build>
    <pluginManagement>
      <pluginswww.devze.com>
        <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>TdgeRkgYp;
            <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中的默认配置文件也打包 -->
        <jsresources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.yml</includehttp://www.devze.com>
                    <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;
    }
}

配置信息

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

到此这篇关于SpringBoot自定义Starter与自动配置实现方法详解的文章就介绍到这了,更多相关SpringBoot自定义Starter内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号