@Configuration 不在@SpringBootApplication 子package路径下无法自动加载

一、场景如下:
1、使用@SpringBootApplication 主类,注意package

package com.example.quartz;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
import com.example.spring.quartz.config.QuartzConfig;
 
@SpringBootApplication
public class QuartzSampleApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(QuartzSampleApplication.class, args);
    }
 
}
2、使用@Configuration的配置类,注意比较package路径

package com.example.spring.quartz.config;
 
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
 
import com.example.spring.quartz.constant.QuartzConstant;
import com.example.spring.quartz.job.MyJob;
 
@Configuration
public class QuartzConfig {
 
    @Bean(name = "exampleJob")
    public JobDetailFactoryBean exampleJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(MyJob.class);
        jobDetailFactoryBean.setName("myJob1");
        jobDetailFactoryBean.setGroup("group1");
        jobDetailFactoryBean.setDescription("this is my first job");
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setApplicationContextJobDataKey(QuartzConstant.QUARTZ_SPRING_CONTEXT_KEY);
        JobDataMap dataMap = new JobDataMap();
        dataMap.put("say", "Hello World");
        jobDetailFactoryBean.setJobDataMap(dataMap);
        return jobDetailFactoryBean;
    }
    
    @Bean(value = "exampleJobTrigger")
    public CronTriggerFactoryBean exampleJobTrigger(@Qualifier(value = "exampleJob") JobDetail jobDetail) {
        CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
        triggerFactoryBean.setName("task1");
        triggerFactoryBean.setGroup("group1");
        triggerFactoryBean.setCronExpression("0 0/1 * * * ?");
        //triggerFactoryBean.setMisfireInstruction(misfireInstruction);
        triggerFactoryBean.setPriority(5);
        triggerFactoryBean.setDescription("this is my ...");
        triggerFactoryBean.setJobDetail(jobDetail);
        return triggerFactoryBean;
    }
}
基本情况就这样,启动时无法加载QuartzConfig 类中声明的@Bean

二、分析原因
找了一会发现是package路径的问题,如果将QuartzConfig类的package改为com.example.quartz或com.example.quartz地子package就能加载QuartzConfig类中声明的@Bean。@SpringBootApplication中包含@EnableAutoConfiguration,会自动加载

声明@SpringBootApplication的类的package及其子package下的@Configuration类,创建Bean。所以,如果带有@Configuration的配置类不在声明@SpringBootApplication的类的package及其子package下是不会自动加载。

 

三、解决办法
方法一:将带有@Configuration的配置类移动到声明@SpringBootApplication的类的package及其子package下

方法二:使用@Import或@ComponentScan

package com.example.quartz;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
 
import com.example.spring.quartz.config.QuartzConfig;
 
@SpringBootApplication
@Import(value = { QuartzConfig.class })
public class QuartzSampleApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(QuartzSampleApplication.class, args);
    } 

你可能感兴趣的:(Spring,Boot)