springBoot中starter

springBoot项目中引入starter

项目引入xxljob,仅需要导入对应的starter包,即可进行快速开发


	com.ydl
	xxl-job-spring-boot-starter
	0.0.1-SNAPSHOT

xxl-job-spring-boot-starter

目录结构:

springBoot中starter_第1张图片

pom.xml中仅导入对应的xxl-job-spring-boot-autoconfiguration依赖



    
        xxl-job-starter
        com.xiaozhen
        0.0.1-SNAPSHOT
    
    4.0.0

    xxl-job-spring-boot-starter
    
        
            com.ydl
            xxl-job-spring-boot-autoconfiguration
            0.0.1-SNAPSHOT
        
    

pom.properties

version=0.0.1-SNAPSHOT
groupId=com.xiaozhen
artifactId=xxl-job-spring-boot-starter

xxl-job-spring-boot-autoconfiguration

目录文件

springBoot中starter_第2张图片

 xxlJobAutoConfiguration为配置类,springBoot启动时会加重

/**
     * 注解解释:
     *
     * @Configuration  //指定这个类是一个配置类
     * @ConditionalOnXXX  //在指定条件成立的情况下自动配置类生效
     * @AutoConfigureAfter  //指定自动配置类的顺序
     * @Bean  //给容器中添加组件
     * @ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
     * @EnableConfigurationProperties //让xxxProperties生效加入到容器中
     */
@Component
@EnableConfigurationProperties({XxlJobProperties.class})
public class XxlJobAutoConfiguration {
    private static Logger logger = LoggerFactory.getLogger(XxlJobAutoConfiguration.class);
    @Resource
    private XxlJobProperties xxlJobProperties;

    public XxlJobAutoConfiguration() {
    }
    
    /**
     * 配置信息
     **/
    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        logger.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(this.xxlJobProperties.getAdminAddresses());
        xxlJobSpringExecutor.setAppName(this.xxlJobProperties.getExecutorAppName());
        xxlJobSpringExecutor.setIp(this.xxlJobProperties.getExecutorIp());
        xxlJobSpringExecutor.setPort(this.xxlJobProperties.getExecutorPort());
        xxlJobSpringExecutor.setAccessToken(this.xxlJobProperties.getAccessToken());
        xxlJobSpringExecutor.setLogPath(this.xxlJobProperties.getExecutorLogPath() + this.xxlJobProperties.getExecutorAppName());
        xxlJobSpringExecutor.setLogRetentionDays(this.xxlJobProperties.getExecutorLogRetentionDays());
        return xxlJobSpringExecutor;
    }
}

xxlJobProperties

@ConfigurationProperties(
    prefix = "xxl.job"
)
public class XxlJobProperties {
    private String adminAddresses = "http://localhost:8093/xxl-job-admin";
    private String accessToken;
    private String executorAppName = "xxl-job-executor-default";
    private String executorIp;
    private int executorPort = 9999;
    private String executorLogPath = "/data/applogs/xxl-job/jobhandler/";
    private int executorLogRetentionDays = 30;

    public XxlJobProperties() {
    }
}

pom.xml导入相关的jar包



    4.0.0
    
        xxl-job-starter
        com.xiaozhen
        0.0.1-SNAPSHOT
    
    com.ydl
    xxl-job-spring-boot-autoconfiguration
    0.0.1-SNAPSHOT
    xxl-job-spring-boot-autoconfiguration
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        

        
        
            com.xuxueli
            xxl-job-core
            2.1.1
        
    

spring.factories文件(spring启动的时候会加载此文件中的配置类)

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ydl.xxljobspringbootautoconfiguration.XxlJobAutoConfiguration

另外这个spring.factories文件还可配置监听类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alibaba.boot.dubbo.autoconfigure.DubboAutoConfiguration


org.springframework.context.ApplicationListener=\
com.alibaba.boot.dubbo.context.event.OverrideDubboConfigApplicationListener,\
com.alibaba.boot.dubbo.context.event.WelcomeLogoApplicationListener,\
com.alibaba.boot.dubbo.context.event.AwaitingNonWebApplicationListener

springBoot中starter_第3张图片

public class AwaitingNonWebApplicationListener implements ApplicationListener {

@Order //最低优先级确保最后执行
public class OverrideDubboConfigApplicationListener implements ApplicationListener {


@Order(LoggingApplicationListener.DEFAULT_ORDER + 1)
public class WelcomeLogoApplicationListener implements ApplicationListener {

你可能感兴趣的:(spring框架,spring,boot,java,spring)