SpringBoot 2.7.8 自定义 Starter &自动配置

文章目录

  • SpringBoot 2.7.8 自定义 Starter
    • 前言
    • 本次练习的代码仓库
    • 代码简要说明
      • custom-springboot-starter-demo 的pom文件
      • customer-starter 的pom文件
      • test 的pom文件
      • 配置类
      • 配置信息

SpringBoot 2.7.8 自定义 Starter

前言

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

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

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

SpringBoot 2.7.8 自定义 Starter &自动配置_第1张图片

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 2.7.8 自定义 Starter &自动配置_第2张图片

SpringBoot 2.7.8 自定义 Starter &自动配置_第3张图片

本次练习的代码仓库

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.0modelVersion>

  <groupId>org.examplegroupId>
  <artifactId>custom-springboot-starter-demoartifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>pompackaging>

  <name>custom-springboot-starter-demoname>
  <url>http://maven.apache.orgurl>
  <modules>
    <module>custom-startermodule>
    <module>testmodule>
  modules>

  <properties>
    <java.version>11java.version>
    <maven.compiler.source>11maven.compiler.source>
    <maven.compiler.target>11maven.compiler.target>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <springboot.dependencies.version>2.7.8springboot.dependencies.version>
  properties>

  <dependencyManagement>
    <dependencies>
      
      <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-dependenciesartifactId>
        <version>${springboot.dependencies.version}version>
        <type>pomtype>
        <scope>importscope>
      dependency>
    dependencies>
  dependencyManagement>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.pluginsgroupId>
          <artifactId>maven-compiler-pluginartifactId>
          <version>3.8.1version>
          <configuration>
            <source>11source>
            <target>11target>
            <encoding>UTF-8encoding>
          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.0modelVersion>
    <parent>
        <groupId>org.examplegroupId>
        <artifactId>custom-springboot-starter-demoartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>custom-starterartifactId>
    <packaging>jarpackaging>

    <name>custom-startername>
    <url>http://maven.apache.orgurl>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-configuration-processorartifactId>
            <optional>trueoptional>
        dependency>
    dependencies>

    <build>
        
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.ymlinclude>
                    <include>**/*.yamlinclude>
                includes>
                <filtering>falsefiltering>
            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.0modelVersion>
    <parent>
        <groupId>org.examplegroupId>
        <artifactId>custom-springboot-starter-demoartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>testartifactId>
    <packaging>jarpackaging>

    <name>testname>
    <url>http://maven.apache.orgurl>


    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.examplegroupId>
            <artifactId>custom-starterartifactId>
            <version>1.0-SNAPSHOTversion>
        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中不做配置时,会抛出警告 Application context not configured for this file
 *
 * @version V1.0
 * @author: fengjinsong
 * @date: 2023年02月02日 10时03分
 */
@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 2.7.8 自定义 Starter &自动配置_第4张图片

你可能感兴趣的:(web框架学习,java练习,spring,boot,spring,java,自定义starter,自动配置)