目录
[1 新建一个springboot项目](#1 新建一个springboot项目)
[2 打开pom,去掉spring-boot-starter-parent](#2 打开pom,去掉spring-boot-starter-parent)
[3 创建如下类](#3 创建如下类)
[4 创建如下配置文件](#4 创建如下配置文件)
[5 最后安装到本地:](#5 最后安装到本地:)
[6 使用starter](#6 使用starter)
[· 可能遇到的问题](#· 可能遇到的问题)
1 新建一个springboot项目

2 打开pom,去掉spring-boot-starter-parent

添加如下依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>3.3.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>3.3.12</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.3.12</version>
</plugin>
</plugins>
</build>
3 创建如下类

java
@ConfigurationProperties(prefix = "your")
public class AgeProperties {
private String birthday;
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
java
public class AgeService {
public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
private final String birthday;
public AgeService(String birthday) {
this.birthday = birthday;
}
public int computeAgeByBirthday() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT);
LocalDate birthDate = LocalDate.parse(birthday, formatter);
LocalDate currentDate = LocalDate.now();
// 检查生日是否在当前日期之后
if (birthDate.isAfter(currentDate)) {
return -1; // 无效的生日
}
// 计算年龄
Period period = Period.between(birthDate, currentDate);
return period.getYears();
}
}
java
// 将当前类声明为配置类
@Configuration
// 如果当前类路径下有 AgeService 这个类,就把当前类加入 IoC 容器
// 当前类路径下怎么才能有 AgeService?引入我们正在开发的这个 starter 的依赖即可
@ConditionalOnClass(AgeService.class)
// 注册属性组件,Spring Boot 会读取用户在配置文件中指定的生日字符串,注入属性组件
@EnableConfigurationProperties(AgeProperties.class)
public class AgeAutoConfiguration {
private final AgeProperties ageProperties;
// 基于构造器装配,把属性组件注入进来
public AgeAutoConfiguration(AgeProperties ageProperties) {
this.ageProperties = ageProperties;
}
@Bean
// Missing 单词:丢失、遗漏、找不到
// MissingBean 单词:IoC 容器中没有这个 bean
// @ConditionalOnMissingBean 条件注解:当 IoC 容器中没有 AgeService 这个 bean 时,才把当前方法返回的对象加入 IoC 容器
// 提问:IoC 容器中为啥会有 AgeService 这个 bean 呢?
// 答:开发人员有可能自己把它加入 IoC 容器
@ConditionalOnMissingBean
public AgeService ageService() {
// 创建业务组件对象的同时,把生日字符串传入进去
return new AgeService(ageProperties.getBirthday());
}
}
@ConditionalOnMissingBean是 Spring Boot 框架提供的一个条件注解,它用于控制 Bean 的创建条件。当容器中不存在指定类型或名称的 Bean 时,被该注解标注的 Bean 才会被创建。这个注解在自动配置场景中尤为重要,它允许框架提供默认实现,同时允许用户通过自定义 Bean 来覆盖默认配置
4 创建如下配置文件

功能:让 Spring Boot 加载我们创建的自动配置类
org.springframework.boot.autoconfigure.AutoConfiguration.imports
注意路径 src/main/resources/META-INF/spring
文件内容,是自动配置类的全限定类名:
com.xianwang.springbootagestarterdemo.config.AgeAutoConfiguration
5 最后安装到本地:

找到本地仓库,按照项目包名 找到生成的jar包

6 使用starter
在一个新项目中引入依赖

注入service,执行调用即可


· 可能遇到的问题
1 启动类pom中一定不要 spring-boot-starter-parent,也没必要保留 SpringbootApplication
2 install时找不到jre
Setting-> Maven-> Runner 配置好即可
