自定义一个starter

        在Spring Boot中,创建一个自定义starter可以简化特定功能或组件的配置过程,让其他项目能够轻松地重用这些功能。

这里我们以自定义一个xxl-job的starter为例,介绍下如何简化配置。

添加依赖

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    
    <!-- 其他需要的依赖 -->
</dependencies>

实现自动配置

        

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

/**
 * 
 */
@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;

/**
 * 
 */
@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的时候才会生效。

在你的starter项目的src/main/resources下,创建META-INF/spring目录,并且创建一个
org.springframework.boot.autoconfigure.AutoConfiguration.imports文件,内容如下:

cn.hollis.nft.turbo.job.config.XxlJobConfiguration

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

相关推荐

  1. 定义一个starter

    2024-05-16 15:18:13       32 阅读
  2. SpringBoot--定义starter

    2024-05-16 15:18:13       49 阅读
  3. springboot定义starter

    2024-05-16 15:18:13       43 阅读
  4. SpringBoot定义Starter

    2024-05-16 15:18:13       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-16 15:18:13       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-16 15:18:13       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-16 15:18:13       82 阅读
  4. Python语言-面向对象

    2024-05-16 15:18:13       91 阅读

热门阅读

  1. ESP32 Arduino 定时器中断

    2024-05-16 15:18:13       35 阅读
  2. vue3-响应式API(工具函数)-unRef

    2024-05-16 15:18:13       37 阅读
  3. 【数据库】高并发场景下的数据库开发注意要点

    2024-05-16 15:18:13       37 阅读
  4. 什么是Vue.js? Vue.js简介

    2024-05-16 15:18:13       32 阅读
  5. SpringBoot解析MyBatis预编译SQL

    2024-05-16 15:18:13       29 阅读