# custom-springboot-starter-demo **Repository Path**: fengsoshuai/custom-springboot-starter-demo ## Basic Information - **Project Name**: custom-springboot-starter-demo - **Description**: 自定义springboot-starter,并测试 版本 2.7.8 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-02-02 - **Last Updated**: 2023-02-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: springboot-starter, 自定义 ## README [TOC] # SpringBoot 2.7.8 自定义 Starter ## 前言 前段时间,SpringBoot 出 3.x 版本了。听说把自动配置给刀了!!(3.x版本不再使用 spring.factories做自动配置) 但是这个在真正开始说要弃用,是在 2.7版本。只是向下兼容了 spring.factories 的配置方式。 也就是说两种写法共存,如下图: ![20230202110640](docs\20230202110640.png) 在 `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 的源码包中看到: ![20230202111302](docs\20230202111302.png) ![20230202111335](docs\20230202111335.png) ## 本次练习的代码仓库 https://gitee.com/fengsoshuai/custom-springboot-starter-demo.git ## 代码简要说明 | 模块 | 说明 | | ---------------- | ------------------------------------------------------------ | | customer-starter | 自定义的 starter,并提供配置、示例接口&实现类 | | test | 测试自定义starter,引入自定义starter的依赖,并定义了启动类,控制器类 | ### custom-springboot-starter-demo 的pom文件 主要定义了 SpringBoot的版本。 ```xml 4.0.0 org.example custom-springboot-starter-demo 1.0-SNAPSHOT pom custom-springboot-starter-demo http://maven.apache.org custom-starter test 11 11 11 UTF-8 UTF-8 UTF-8 2.7.8 org.springframework.boot spring-boot-dependencies ${springboot.dependencies.version} pom import org.apache.maven.plugins maven-compiler-plugin 3.8.1 11 11 UTF-8 ``` ### customer-starter 的pom文件 ```xml 4.0.0 org.example custom-springboot-starter-demo 1.0-SNAPSHOT custom-starter jar custom-starter http://maven.apache.org org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-configuration-processor true src/main/resources **/*.properties **/*.yml **/*.yaml false ``` ### test 的pom文件 ```xml 4.0.0 org.example custom-springboot-starter-demo 1.0-SNAPSHOT test jar test http://maven.apache.org org.springframework.boot spring-boot-starter-web org.example custom-starter 1.0-SNAPSHOT ``` ### 配置类 ```java 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; } } ``` ### 配置信息 ![20230202113605](docs\20230202113605.png)