当前位置: 首页 > article >正文

自定义SpringBoot Starter

✅自定义SpringBoot Starter

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



1、添加依赖



添加Spring Boot的依赖:

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

2、实现自动配置



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



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:



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.kler.cn/a/560383.html

相关文章:

  • 1.✨Java学习笔记
  • Win10登录Samba服务器报用户名密码错误问题解决
  • Windows 11【1001问】如何下载Windows 11系统镜像
  • 安装可视化jar包部署平台JarManage
  • 【排序算法】堆排序详解
  • 金融行业数据安全:KSP密钥管理系统如何保障支付交易与客户信息零泄露
  • springcloud负载均衡策略有哪些
  • 芯谷D1308:低成本、高性能的便携式音频解决方案
  • 【数据处理】COCO 数据集掩码 Run-Length Encoding (RLE) 编码转二进制掩码
  • UE5 Gameplay框架及继承关系详解
  • WPF基本布局基础
  • 【无人集群系列---大疆无人集群技术进展、技术路线与未来发展方向】
  • Hi3516CV610开发板ISP调试之——图像ISP在线调试 环境搭建教程
  • 理解CompletableFuture的非阻塞
  • springboot005学生心理咨询评估系统(源码+数据库+文档)
  • 使用Vue-Flow创建一个流程图可视化节点坐标查询器
  • 【算法】位运算
  • 4. Spring Cloud Gateway 入门与使用
  • 牛客周赛 Round 82(思维、差分、树状数组、大根堆、前后缀、递归)
  • JavaWeb基础专项复习4——会话对象Session and Cookie