Spring Boot因为没有在启动类中@Import 带@Configuration注解的配置类而导致无法扫描到该配置类

在项目中新增了如下配置类

@Configuration
public class SchedulerConfig {
    
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setQuartzProperties(quartzProperties());
        return factory;
    }
    
    @Bean
    public Properties quartzProperties() throws IOException {
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
//      propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
//      //在quartz.properties中的属性被读取并注入后再初始化对象
//      propertiesFactoryBean.afterPropertiesSet();
        return propertiesFactoryBean.getObject();
    }
    
    /**
     * 通过SchedulerFactoryBean获取Scheduler的实例
     * @author 北北
     * @date 2018年6月15日上午10:51:54
     * @return
     * @throws IOException
     */
    @Bean
    public Scheduler scheduler() throws IOException {
        return schedulerFactoryBean().getScheduler();
    }
    
    /**
     * quartz初始化监听器
     * @author 北北
     * @date 2018年6月15日上午10:51:59
     * @return
     */
    @Bean
    public QuartzInitializerListener executorListener() {
       return new QuartzInitializerListener();
    }
    
}

因为有一些地方需要依赖SchedulerFactoryBean注入,然后项目启动失败说找不到SchedulerFactoryBean的实体对象,仔细检查后发现是因为没有把该SchedulerConfig配置类引入Application启动类中,然后在Application启动类中引入该配置类后就好了,Application类引入该配置类代码如下:

@SpringBootApplication
@EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class})
@ComponentScan
@Configuration
@Import({
        SpringBootServiceConfig.class, WebConfig.class, SchedulerConfig.class
})
@EnableDiscoveryClient
public class Application extends  SpringBootServletInitializer{


    private static Logger logger = Logger.getLogger(Application.class);

    public static void main(final String[] args) {
        SpringApplication.run(Application.class, args);
        logger.info("---------------------------------------------------SpringBoot Start Success-----------------------------------------------------------------");
    }


}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容