自定义SpringBoot Starter

ops/2025/2/25 13:03:25/

✅自定义SpringBoot Starter

SpringBoot 的 starter 可以帮我们简化配置,非常的方便,定义起来其实也不复杂,我的项目中定义了很多 starter,比如business-job就是一个 stater,以他为例,介绍下如何定义 starter:



1、添加依赖



添加Spring Boot的依赖:

java"><dependencies>
​<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency>
​
</dependencies>
​

2、实现自动配置



在starter项目中,创建自动配置类。这个类要使用@Configuration注解,并根据条件使用@ConditionalOn...注解来条件化地配置beans。



java">import org.springframework.boot.context.properties.ConfigurationProperties;
​
/*** @author wzc*/
@ConfigurationProperties(prefix = XxlJobProperties.PREFIX)
public class XxlJobProperties {
​public static final String PREFIX = "spring.xxl.job";
​private boolean enabled;
​private String adminAddresses;
​private String accessToken;
​private String appName;
​private String ip;
​private int port;
​private String logPath;
​private int logRetentionDays = 30;
​//getter setter
}


接下来定义Configuration,并且在其中创建需要的bean:



java">import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
​
/*** @author wzc*/
@Configuration
@EnableConfigurationProperties(XxlJobProperties.class)
public class XxlJobConfiguration {
​private static final Logger logger = LoggerFactory.getLogger(XxlJobConfiguration.class);
​@Autowiredprivate XxlJobProperties properties;
​@Bean@ConditionalOnMissingBean@ConditionalOnProperty(prefix = XxlJobProperties.PREFIX, value = "enabled", havingValue = "true")public XxlJobSpringExecutor xxlJobExecutor() {logger.info(">>>>>>>>>>> xxl-job config init.");XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();xxlJobSpringExecutor.setAdminAddresses(properties.getAdminAddresses());xxlJobSpringExecutor.setAppname(properties.getAppName());xxlJobSpringExecutor.setIp(properties.getIp());xxlJobSpringExecutor.setPort(properties.getPort());xxlJobSpringExecutor.setAccessToken(properties.getAccessToken());xxlJobSpringExecutor.setLogPath(properties.getLogPath());xxlJobSpringExecutor.setLogRetentionDays(properties.getLogRetentionDays());return xxlJobSpringExecutor;}
}



这里面用@Bean 注解声明了一个bean,并且使用@ConditionalOnMissingBean类指定这个bean的创建条件,即在确实的时候创建。



@ConditionalOnProperty(prefix = XxlJobProperties.PREFIX, value = "enabled", havingValue = "true")约定了当我们配置了spring.xxl.job.enable=true的时候才会生效。



3、创建配置类入口文件



在你的starter项目的src/main/resources下,创建META-INF/spring目录,并且创建一个

org.springframework.boot.autoconfigure.AutoConfiguration.imports文件,内容如下:



cn.business.job.config.XxlJobConfiguration



以上就定义好了一个starter,只需要在需要的地方引入,并且配置上相应的配置项就行了,配置项内容就是我们定义在XxlJobProperties中的。



以前,我们配置这些Configuration的时候会用spring.factories,但是这个已经被官方标记为过期,不建议使用了。


http://www.ppmy.cn/ops/161204.html

相关文章

《炒股养家心法.pdf》 kimi总结

《炒股养家心法.pdf》这篇文章详细阐述了一位超级游资炒股养家的心得与技巧&#xff0c;展示了其从40万到10亿的股市传奇。以下是文章中炒股技巧和心得的详细总结&#xff1a; 1.核心理念 市场情绪的理解&#xff1a;炒股养家强调&#xff0c;股市的本质是群体博弈&#xff0c…

在 Mac mini M2 上本地部署 DeepSeek-R1:14B:使用 Ollama 和 Chatbox 的完整指南

随着人工智能技术的飞速发展&#xff0c;本地部署大型语言模型&#xff08;LLM&#xff09;已成为许多技术爱好者的热门选择。本地部署不仅能够保护隐私&#xff0c;还能提供更灵活的使用体验。本文将详细介绍如何在 Mac mini M2&#xff08;24GB 内存&#xff09;上部署 DeepS…

Python常见面试题的详解16

1. 如何强行关闭客户端和服务器之间的连接&#xff1f; 在网络编程中&#xff0c;有时需要强行中断客户端和服务器之间的连接。对于基于 TCP 协议的连接&#xff0c;由于其面向连接的特性&#xff0c;需要采取特定的步骤来确保连接被正确关闭&#xff1b;而 UDP 是无连接协议&a…

AI回答:Linux C/C++编程学习路线

Linux C/C编程学习路线需要结合Linux系统特性和C/C语言的特点&#xff0c;以下是一个系统化的学习路径&#xff0c;适合从初学者到进阶者&#xff1a; 第一阶段&#xff1a;Linux基础 Linux操作系统基础 学习Linux基本命令&#xff1a;ls、cd、mkdir、rm、grep、find等。 理解…

向日葵linux端ubuntu24.04LTS安装解决方案

项目场景&问题描述 提示&#xff1a;这里描述项目中遇到的问题&#xff1a; 两台主机&#xff1a;一台win10, ubuntu24.04 &#xff0c;我想实现相应的相互控制&#xff0c;因为我只有一个显示屏&#xff0c;笔记本&#xff0c;屏幕不够大&#xff0c;很不爽。 尝试的方法…

Python爬虫(四)- Selenium 安装与使用教程

文章目录 前言一、简介及安装1. Selenium 简介2. Selenium 安装 二、Selenium 基本使用1. 导入Selenium2. 启动浏览器3. 打开网页4. 获取页面标题5. 关闭浏览器6. 完整示例代码 三、Selenium WebDriver1. 简介2. 基本操作2.1 启动浏览器2.2 关闭浏览器2.3 打开网页2.4 关闭当前…

详解Tomcat下载安装以及IDEA配置Tomcat(2023最新)

目录 步骤一&#xff1a;首先确认自己是否已经安装JDK步骤二&#xff1a;下载安装Tomcat步骤三&#xff1a;Tomcat配置环境变量步骤四&#xff1a;验证Tomcat配置是否成功步骤五&#xff1a;为IDEA配置Tomcat 步骤一&#xff1a;首先确认自己是否已经安装JDK jdk各版本通用安…

白帽黑客系列教程之Windows驱动开发(64位环境)入门教程(二)

为什么要写这篇文章呢&#xff1f; 作为一名白帽黑客&#xff0c;如果想要学习ROOTKIT攻防技术&#xff0c;就必须要有能力进行驱动开发&#xff01; 本文章仅提供学习&#xff0c;切勿将其用于不法手段&#xff01; 在Windows操作系统的64位环境中&#xff0c;进行ROOTKIT攻…