package com.example.quartztest.demo.config; import com.example.quartztest.demo.Job.ScheduleTask; import org.quartz.Trigger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.quartz.CronTriggerFactoryBean; import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean; import org.springframework.scheduling.quartz.SchedulerFactoryBean; @Configuration public class QuartzConfigration { @Bean(name = "jobDetail") public MethodInvokingJobDetailFactoryBean detailFactoryBean(ScheduleTask task) { // ScheduleTask为需要执行的任务 MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean(); /* * 是否并发执行 * 例如每5s执行一次任务,但是当前任务还没有执行完,就已经过了5s了, * 如果此处为true,则下一个任务会bing执行,如果此处为false,则下一个任务会等待上一个任务执行完后,再开始执行 */ jobDetail.setConcurrent(true); jobDetail.setName("scheduler");// 设置任务的名字 jobDetail.setGroup("scheduler_group");// 设置任务的分组,这些属性都可以存储在数据库中,在多任务的时候使用 /* * 这两行代码表示执行task对象中的scheduleTest方法。定时执行的逻辑都在scheduleTest。 */ jobDetail.setTargetObject(task); jobDetail.setTargetMethod("scheduleTest"); return jobDetail; } @Bean(name = "jobTrigger") public CronTriggerFactoryBean cronJobTrigger(MethodInvokingJobDetailFactoryBean jobDetail) { CronTriggerFactoryBean tigger = new CronTriggerFactoryBean(); tigger.setJobDetail(jobDetail.getObject()); tigger.setCronExpression("0/6 * * * * ?");// 表示每隔6秒钟执行一次 //tigger.set tigger.setName("myTigger");// trigger的name return tigger; } @Bean(name = "scheduler") public SchedulerFactoryBean schedulerFactory(Trigger cronJobTrigger) { SchedulerFactoryBean bean = new SchedulerFactoryBean(); //设置是否任意一个已定义的Job会覆盖现在的Job。默认为false,即已定义的Job不会覆盖现有的Job。 bean.setOverwriteExistingJobs(true); // 延时启动,应用启动5秒后 ,定时器才开始启动 bean.setStartupDelay(5); // 注册定时触发器 bean.setTriggers(cronJobTrigger); return bean; } }
package com.example.quartztest.demo.Job; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.stereotype.Component; /** * @ClassName ScheduleTask * @Description TODO * @Author ChenGeng * @Date 2019/2/27 15:51 */ @Configuration @Component @EnableScheduling // 该注解必须要加 public class ScheduleTask { public void scheduleTest() { System.err.println("scheduleTest开始定时执行"); } }
package com.example.quartztest.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scheduler' defined in class path resource [com/example/quartztest/demo/config/QuartzConfigration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.scheduling.quartz.SchedulerAccessor] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
我的格式是这样啊,仅供参考。。。
@Configuration
@Component
@EnableScheduling
@DisallowConcurrentExecution
public class XxJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
}
}
还是没用,一直提示Error creating bean with name 'scheduler' defined in class path resource [com/example/quartztest/demo/config/QuartzConfigration.class]
@青山与妙高: 最后怎么解决的,我现在也遇到这个问题了
@lana不分: 请问一下,您的这个问题是怎么解决的
没有找到问题的答案。。
解决了吗?