Spring Boot 3 自定义Starter

一、软件版本:

Java: 17

Spring Boot: 3.0.5

二、创建工程

Starter分为两个模块,一个是自动配置模块,另一个是启动程序模块。根据实际情况这两个模块也可以为一个。

1、创建message-spring-boot-autoconfigure工程

 

 2、配置pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>3.0.5</version>

<relativePath /> <!-- lookup parent from repository -->

</parent>

<groupId>stu.sis</groupId>

<artifactId>message-spring-boot-autoconfigure</artifactId>

<version>0.0.1-SNAPSHOT</version>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<java.version>17</java.version>

</properties>

<dependencies>

<!--Spring Boot自动配置依赖-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-autoconfigure</artifactId>

<optional>true</optional>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

</dependency>

</dependencies>

</project>

3 创建MessagePropertie类

package stu.sis;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "message")

public class MessageProperty {

private String title;

private String adress;

private String sendBy;

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getAdress() {

return adress;

}

public void setAdress(String adress) {

this.adress = adress;

}

public String getSendBy() {

return sendBy;

}

public void setSendBy(String sendBy) {

this.sendBy = sendBy;

}

}

说明:

注解:@ConfigurationProperties

       在 SpringBoot 中,当想要获取到配置文件数据时,除了可以用 Spring 自带的 @Value 注解外,SpringBoot 还提供了一种更加方便的方式:@ConfigurationProperties。只要在 Bean 上添加上了这个注解,指定好配置文件的前缀,那么对应的配置文件数据就会自动填充到 Bean 中。

   在普通开发中可以跟@Component@Service组合,但是在Starter开发中必须和@EnableConfigurationProperties组合,@Component@Service如果用为第三方包引入时很可能扫描不到

4、创建MessageService类

package stu.sis;

public class MessageService {

private MessageProperty messageProperty;

public MessageService(MessageProperty messageProperty) {

this.messageProperty = messageProperty;

}

public boolean send(String context,String adress) {

System.out.println("发信息内容=>"+context);

System.out.println("信息由["+this.messageProperty.getAdress()+"]发送给=>["+adress+"]");

return true;

}

}

5、创建MessageConfiguration类

package stu.sis;

import org.springframework.boot.context.properties.EnableConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration

@EnableConfigurationProperties(MessageProperty.class)

public class MessageConfiguration {

private MessageProperty messageProperty;

public MessageConfiguration(MessageProperty messageProperty) {

this.messageProperty = messageProperty;

}

@Bean

public MessageService messageService() {

return new MessageService(this.messageProperty);

}

}

说明:

@EnableConfigurationProperties

注解作用是把带有 @ConfigurationProperties 的类注入IOC容器中。

注解@EnableConfigurationProperties(A.class)的作用就是如果A 这个类上使用了@ConfigurationProperties 注解,那么 A 这个类会与 xxx.properties 进行动态绑定,并且会将 A 这个类加入 IOC 容器中,并交由 IOC 容器进行管理,如果A类上没有@ConfigurationProperties就会报错。

5、创建配置文件

message.title=CSSCA

message.adress=911@126.com

message.sendBy=XIAO LI

6、创建配置类

      在resources目录下创建META-INF/spring/文件夹,在这个目录下创建文件org.springframework.boot.autoconfigure.AutoConfiguration.imports,在这个文件配置装配类

stu.sis.MessageConfiguration

这是Spring Boot 3.x 最大的区别。

7、打包程序

8、创建message-spring-boot-starter

9、创建pom.xml文件

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>3.0.5</version>

<relativePath /> <!-- lookup parent from repository -->

</parent>

<groupId>stu.sis</groupId>

<artifactId>message-spring-boot-starter</artifactId>

<version>0.0.1-SNAPSHOT</version>

<dependencies>

<dependency>

<groupId>stu.sis</groupId>

<artifactId>message-spring-boot-autoconfigure</artifactId>

<version>0.0.1-SNAPSHOT</version>

</dependency>

</dependencies>

</project>

10、创建测试类

package stu.sis;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest

@SpringBootApplication

public class MessageAutoTest {

        @Autowired

        private MessageService messageService;

        @Test

        public void sengMessage() {

                 messageService.send("Hello world!","111@126.com");

        }

}

11、测试结果

补充知识

定义:

       启动器是一组相关依赖项描配置,你可以在应用中引用它。你可以通过一站式配置获得所有Spring和相关技术,而不需要查找代码例子和复制-粘贴大量依赖配置,例如,如果你想用SpringJPA进行数据库访问,在项目中引入Spring -boot-starter-data- JPAStarter依赖。

典型的Spring Boot Starter包含用于自动配置和自定义特定技术的基础架构的代码,我们称其为“acme”。为了使其易于扩展,可以将专用命名空间中的许多配置项公开给环境。最后,提供了一个 starter 依赖项,以帮助用户尽可能轻松地使用它。

自定义启动器可以包含两个模块:

  • 自动配置模块,包含“acme”的自动配置代码。
  • 启动程序模块,它提供对自动配置模块的依赖以及“acme”和通常有用的任何其他依赖。简而言之,添加启动程序应提供开始使用该库所需的一切。

       如果“acme”有几种选项或可选功能,那么最好将自动配置分开,因为你可以清楚地表达一些功能是可选的。此外,您还可以制作一个启动器,以提供有关那些可选依赖项的意见。同时,其他人只能依靠自动配置模块并以不同的意见来制作自己的启动器。如果自动配置相对简单并且不具有可选功能,则将两个模块合并在启动器中绝对是一种选择。

命名

      自定义Starter你需要确保有一个合适的命名空间。不要用spring-boot命名你的模块名,即使你使用不同的Maven groupId

假设你正在为“acme”创建starter,并将自动配置模块命名为acme-spring-boot,将starter命名为acme-spring-boot-starter。如果只有一个模块结合了这两个模块,请将其命名为acme-spring-boot-starter

配置key

      如果Starter提供配置key,需要确保命名空间唯一。特别是,不要在Spring Boot使用的命名空间中包含你的键(比如:server, management, spring等等)。如果你使用相同的命名空间,我们可能会在将来以破坏模块的方式修改这些命名空间。根据经验,使用你拥有的名称空间(例如acme)为所有键添加前缀。

  • 20
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值