快速编写springBoot-starter插件

 

第一步 引入starter相关的maven依赖


    org.springframework.boot
    spring-boot-starter-web


    org.springframework.boot
    spring-boot-starter


    org.springframework.boot
    spring-boot-configuration-processor
    true


    org.springframework.boot
    spring-boot-autoconfigure

第二步 创建starter所需要的自动装配的类例如

@Configuration//必须加
@EnableAutoConfiguration//启动自动装配必须加
@EnableConfigurationProperties({JobProperty.class})//引入相关的配置类可选
@Slf4j
public class JobAutoConfiguration {

//以下是改starter项目的需要的注入的bean
    @Bean
    @ConditionalOnMissingBean
    public JobProperty jobProperty() {
        return new JobProperty();
    }

    @Bean(initMethod = "start", destroyMethod = "destroy")
    public XxlJobSpringExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(jobProperty().getAdminAddresses());
        xxlJobSpringExecutor.setAppName(jobProperty().getAppName());
        xxlJobSpringExecutor.setIp(jobProperty().getIp());
        xxlJobSpringExecutor.setPort(jobProperty().getPort());
        xxlJobSpringExecutor.setAccessToken(jobProperty().getAccessToken());
        xxlJobSpringExecutor.setLogPath(jobProperty().getLogPath());
        xxlJobSpringExecutor.setLogRetentionDays(jobProperty().getLogRetentionDays());

        return xxlJobSpringExecutor;
    }
}

第三步 resource 下创建MATE-INF文件夹创建spring.factories文件

添加内容为

org.springframework.boot.autoconfigure.EnableAutoConfiguration=//先前编写好的自动装配的类,包名路径如 cn.xx.xxx.JobAutoConfiguration 

第四步 打包,然后在索要使用的项目里加入改项目的maven依赖,便使用。

https://www.bilibili.com/video/BV1Va4y1a72H/

素质三连,新人求关注

你可能感兴趣的:(java)