自定义SpringBoot Starter

news/2025/2/25 5:42:48

✅自定义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);
​
    @Autowired
    private 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.niftyadmin.cn/n/5865035.html

相关文章

[Web 安全] Web 安全攻防 - 学习手册

0x01&#xff1a;Web 安全攻防 —— 信息收集篇 Web 信息收集 — 手动收集域名信息Web 信息收集 — 手动收集 IP 信息Web 信息收集 — 端口服务信息收集 Web 信息收集 — 自动化信息收集 — ShuiZe 水泽Web 信息收集 — 自动化信息收集 — ARL 灯塔 0x02&#xff1a;Web 安全…

设计模式教程:状态模式(State Pattern)

一、概述 状态模式&#xff08;State Pattern&#xff09;是一种行为型设计模式&#xff0c;允许对象在内部状态发生变化时改变其行为。换句话说&#xff0c;状态模式让对象的行为随其状态变化而变化。它的主要目的是将状态相关的行为封装到状态类中&#xff0c;从而避免在类中…

23种设计模式之《外观模式(Facade)》在c#中的应用及理解

程序设计中的主要设计模式通常分为三大类&#xff0c;共23种&#xff1a; 1. 创建型模式&#xff08;Creational Patterns&#xff09; 单例模式&#xff08;Singleton&#xff09;&#xff1a;确保一个类只有一个实例&#xff0c;并提供全局访问点。 工厂方法模式&#xff0…

蓝耘科技发布DeepSeek满血版:引领AI推理革新,开启智慧新时代

引言&#xff1a;人工智能的未来——蓝耘科技的卓越突破 在当今科技飞速发展的时代&#xff0c;人工智能&#xff08;AI&#xff09;技术正深刻地改变着我们生活与工作方式的方方面面。作为AI领域的创新者与领航者&#xff0c;蓝耘科技始终走在技术前沿&#xff0c;凭借无穷的…

Linux系统需要学习的内容和学习计划

掌握Linux系统,需要学习那些内容? 嗯,用户问的是“掌握Linux系统,需要学习那些内容?”。首先,我需要理解用户的需求。用户可能是一个刚开始学习Linux的新手,或者有一定基础但想系统提升的人。他们想知道从哪些方面入手,才能全面掌握Linux系统。 接下来,我得考虑回答…

前缀和刷题-- LeetCode

文章目录 寻找数组的中心下标题解代码 巧克力题解代码 寻找数组的中心下标 题目 题解 1. 预处理前缀和和后缀和数组&#xff0c;注意边界问题&#xff0c;f(0) 0,g(n-1) 0 2. 然后遍历数组一遍&#xff0c;f[i] g[i],i就是下标 3. 时间复杂度是&#xff1a;O(N) 代码 cl…

HandBrake for Mac v1.9.2 视频压缩及格式转换 汉化版 支持M、Intel芯片

HandBrake 是一款开源的视频格式转换压缩工具。基本上支持所有常见的视频转式的转换以及压缩&#xff0c;压缩率高&#xff0c;压缩质量好。 应用介绍 Handbrake Mac是一款适用于Mac操作系统的视频转码器&#xff0c;能够将DVD或普通视频转换为高质量的MP4或MKV。HandBrake ma…

谈谈 ES 6.8 到 7.10 的功能变迁(3)- 查询方法篇

上一篇咱们了解了 ES 7.10 相较于 ES 6.8 新增的字段类型&#xff0c;这一篇我们继续了解新增的查询方法。 Interval 间隔查询&#xff1a; 功能介绍 Interval 查询&#xff0c;词项间距查询&#xff0c;可以根据匹配词项的顺序、间距和接近度对文档进行排名。主要解决的查询…