Spring Boot因为没有在启动类中@Import 带@Co

2018-06-20  本文已影响0人  李北北

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

@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-----------------------------------------------------------------");
    }


}
上一篇 下一篇

猜你喜欢

热点阅读