SpringBoot自定义启动器Starter流程详解
作者:Decade0712 时间:2022-08-25 08:28:19
一、背景
虽然Spring官方给我们提供了很多的启动器供我们使用
但有时候我们也会遇到某些特殊场景,这些启动器满足不了
这个时候就需要自定义一个启动器供我们使用
二、自定义启动器
在之前学习Spring Boot的过程中,我们已经对启动器有了一个大致的了解
Spring Boot实现某个功能,一般是引入对应场景的启动器(一般不写代码,只是声明这个启动器需要引用哪些依赖),然后这个启动器又有对应的自动配置包
1、创建一个启动器的自动配置模块
先写和配置文件中配置项互相绑定的实体类
package com.decade.autoConfigure.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
// 将这个类的属性和系统配置文件中的属性进行绑定
@ConfigurationProperties(prefix = "team")
public class TeamInfo {
private String winnerName;
private String loserName;
public String getWinnerName() {
return winnerName;
}
public void setWinnerName(String winnerName) {
this.winnerName = winnerName;
}
public String getLoserName() {
return loserName;
}
public void setLoserName(String loserName) {
this.loserName = loserName;
}
}
再写自己要实现的业务逻辑
package com.decade.autoConfigure.service;
import com.decade.autofigure.pojo.TeamInfo;
import org.springframework.beans.factory.annotation.Autowired;
public class TestService {
// 引入和yaml文件中配置项绑定的类
@Autowired
private TeamInfo teamInfo;
public String testMethod() {
return teamInfo.getWinnerName() + "今天早上,绝杀了" + teamInfo.getLoserName();
}
}
接着,写一个自动配置类,向容器中放入组件
package com.decade.autoConfigure.auto;
import com.decade.autofigure.pojo.TeamInfo;
import com.decade.autofigure.service.TestService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
// 使得TeamInfo中的@ConfigurationProperties注解生效,将TeamInfo注册到容器中
@EnableConfigurationProperties(TeamInfo.class)
@Configuration
public class AutoConfiguration {
@Bean
// 只有容器中不存在TestService这个类型的bean时,才回去初始化这个bean
@ConditionalOnMissingBean(TestService.class)
public TestService testService() {
TestService testService = new TestService();
return testService;
}
}
最后,在新版本的Spring Boot中为了确保框架启动时加载该配置类
我们需要在这个模块的resource文件下
新建/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
文件
然后在里面写自己之前创建的自动配置类
注意:老版可能是放在META-INF/spring.factories文件中
com.decade.autoConfigure.auto.AutoConfiguration
2、创建一个启动器模块
不用写任何业务代码,只需要在pom文件中,引用之前的创建的自动配置模块
3、在业务模块中引入启动器
如图,引入我们自己定义的启动器
然后在yaml文件中设置好绑定的配置项
再写一个测试方法进行测试即可
package com.decade;
import com.decade.autoConfigure.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@Slf4j
class SpringbootWebApplicationTests {
@Autowired
private TestService testService;
@Test
public void testCustomStarter() {
System.out.println(testService.testMethod());
}
}
测试方法结果如下图
来源:https://blog.csdn.net/Decade0712/article/details/127822532
标签:SpringBoot,自定义,Starter
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
EasyExcel实现导入+各种数据校验功能
2022-05-12 13:05:26
![](https://img.aspxhome.com/file/2023/1/71921_0s.png)
SpringBoot任务之详解邮件任务
2021-08-12 12:49:16
![](https://img.aspxhome.com/file/2023/6/113206_0s.png)
Java Socket编程(四) 重复和并发服务器
2022-06-29 07:07:06
android 仿微信demo——注册功能实现(服务端)
2023-10-30 09:58:23
![](https://img.aspxhome.com/file/2023/8/114328_0s.png)
SpringBoot实现分页功能
2021-11-07 12:33:16
Android Studio中使用jni进行opencv开发的环境配置方法
2021-07-26 00:02:06
![](https://img.aspxhome.com/file/2023/4/102624_0s.jpg)
mybatis中批量插入的两种方式(高效插入)
2023-09-16 22:21:07
C#中XmlTextWriter读写xml文件详细介绍
2022-01-26 05:13:48
c# 如何实现代码生成器
2023-11-13 19:23:35
![](https://img.aspxhome.com/file/2023/0/94540_0s.png)
浅谈java中Math.random()与java.util.random()的区别
2023-11-26 16:37:16
android SDk中常用的java包介绍
2021-12-18 01:04:45
教你java面试时如何聊单例模式
2022-04-14 05:32:57
![](https://img.aspxhome.com/file/2023/6/119186_0s.png)
C#图像处理之图像平移的方法
2021-12-16 08:38:37
Java 实战项目之精美物流管理系统的实现流程
2023-10-30 19:53:46
![](https://img.aspxhome.com/file/2023/8/125248_0s.png)
SpringBoot自定义MessageConverter与内容协商管理器contentNegotiationManager详解
2023-11-28 11:35:54
![](https://img.aspxhome.com/file/2023/5/60285_0s.png)
Android控件实现图片缩放功能
2022-06-10 15:59:27
![](https://img.aspxhome.com/file/2023/8/137798_0s.gif)
详解Kotlin:forEach也能break和continue
2022-05-03 01:24:10
Java 类与对象超基础讲解
2023-06-12 00:03:22
![](https://img.aspxhome.com/file/2023/8/70938_0s.png)
Android Studio中Run按钮是灰色的快速解决方法
2023-12-27 03:06:18
![](https://img.aspxhome.com/file/2023/4/137994_0s.png)
Android中创建对话框(确定取消对话框、单选对话框、多选对话框)实例代码
2023-07-04 01:48:25
![](https://img.aspxhome.com/file/2023/4/137494_0s.gif)