当前位置: 首页 > 知识库问答 >
问题:

Spring Boot+Quartz:“请求的bean当前正在创建中:是否存在不可解析的循环引用?”

宰父疏珂
2023-03-14

该作业工作正常,但每次运行该应用程序时,它都会给出一些警告消息:

[main] WARN  o.s.b.f.s.DefaultListableBeanFactory - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'testJobBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
[main] WARN  o.s.b.f.s.DefaultListableBeanFactory - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'testJobBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
[main] WARN  o.s.b.f.s.DefaultListableBeanFactory - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'testJobBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
[main] WARN  o.s.b.f.s.DefaultListableBeanFactory - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'testJobBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
[main] WARN  o.s.b.f.s.DefaultListableBeanFactory - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'testJobBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
[main] WARN  o.s.b.f.s.DefaultListableBeanFactory - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'testJobBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
[main] WARN  o.s.b.f.s.DefaultListableBeanFactory - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'testJobBeanTrigger': Requested bean is currently in creation: Is there an unresolvable circular reference?


我减少了以下警告消息:@dependson(“testjobbean”)

我注意到job class中的自动服务正在导致它,但为什么呢?我该怎么做才能摆脱这些警告信息?

@Configuration
@ConditionalOnProperty(name = "quartz.enabled")
public class QuartzConfig {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private PlatformTransactionManager transactionManager;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    List<Trigger> triggers;

    @Bean
    public JobFactory jobFactory() {
        AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
        jobFactory.setApplicationContext(applicationContext);
        return jobFactory;
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory) throws IOException {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setOverwriteExistingJobs(true);
        factory.setDataSource(dataSource);
        factory.setTransactionManager(transactionManager);
        factory.setJobFactory(jobFactory);
        factory.setQuartzProperties(quartzProperties());

        if (triggers != null && !triggers.isEmpty()) {
            factory.setTriggers(((Trigger[]) triggers.toArray(new Trigger[triggers.size()])));
        }

        return factory;
    }

    @Bean
    public Properties quartzProperties() throws IOException {
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
        propertiesFactoryBean.afterPropertiesSet();
        return propertiesFactoryBean.getObject();
    }

    public static SimpleTriggerFactoryBean createTrigger(JobDetail jobDetail, long pollFrequencyMs) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(jobDetail);
        factoryBean.setRepeatInterval(pollFrequencyMs);
        factoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT);
        return factoryBean;
    }

    public static CronTriggerFactoryBean createCronTrigger(JobDetail jobDetail, String cronExpression) {
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
        factoryBean.setJobDetail(jobDetail);
        factoryBean.setCronExpression(cronExpression);
        factoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
        return factoryBean;
    }

    public static JobDetailFactoryBean createJobDetail(Class<?> jobClass) {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(jobClass);
        factoryBean.setDurability(true);
        return factoryBean;
    }

}
public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {

    private transient AutowireCapableBeanFactory beanFactory;

    @Override
    public void setApplicationContext(final ApplicationContext context) {
        beanFactory = context.getAutowireCapableBeanFactory();
    }

    @Override
    protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
        final Object job = super.createJobInstance(bundle);
        beanFactory.autowireBean(job);
        return job;
    }
}
@Component
@DisallowConcurrentExecution
public class TestJob implements Job {

    @Autowired
    private TestService service;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        service.execute();
    }

    @Bean(name = "testJobBean")
    public JobDetailFactoryBean createJobDetail() {
        return QuartzConfig.createJobDetail(this.getClass());
    }

    @DependsOn("testJobBean")
    @Bean(name = "testJobBeanTrigger")
    public CronTriggerFactoryBean createTrigger(@Qualifier("testJobBean") JobDetail jobDetail,
            @Value("${quartz.job.test.cronExpression}") String cronExpression) {
        return QuartzConfig.createCronTrigger(jobDetail, cronExpression);
    }

}



MyService.java

@Service
@Slf4j
public class MyService {

    private TestRepository testRepository;

    @Autowired
    public MyService(TestRepository testRepository) {
        this.testRepository = testRepository;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
    public void execute() {
        log.debug("Job has been started");
        List<TestObject> list = testRepository.findAll();
        if (list != null && !list.isEmpty()) {

            System.out.println("\n");
            list.forEach(obj -> {
                System.out.println(
                        "TestObject ID: " + obj.getId() + "\n" + 
                        "SubTestObjects IDs: " + (obj.getSubTestObjects() == null ? "null" : obj.getSubTestObjects().toString()) + 
                        "AnotherSubTestObjects IDs: " + (obj.getAnotherSubTestObjects() == null ? "null" : obj.getAnotherSubTestObjects().toString())
                        );
            });
            System.out.println();

            log.info(list.size() + " test objects found");
        } else {
            log.info("No test objects were found");
        }
        log.debug("Job has been finished");
    }

}



石英.属性

#============================================================================
# Configure Main Scheduler Properties
#============================================================================

org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.makeSchedulerThreadDaemon = true

#============================================================================
# Configure ThreadPool
#============================================================================

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 1
org.quartz.threadPool.makeThreadsDaemons = true

#============================================================================
# Configure JobStore
#============================================================================

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
org.quartz.jobStore.useProperties = true
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.clusterCheckinInterval 3600000

共有1个答案

阴元青
2023-03-14

看来问题可能出在createjobdetail方法中,请考虑以下代码段:

    @Bean(name = "testJobBean")
    public JobDetailFactoryBean createJobDetail() {
        return QuartzConfig.createJobDetail(this.getClass());
    }

    @DependsOn("testJobBean")
    @Bean(name = "testJobBeanTrigger")
    public CronTriggerFactoryBean createTrigger(@Qualifier("testJobBean") JobDetail jobDetail,
            @Value("${quartz.job.test.cronExpression}") String cronExpression) {
        return QuartzConfig.createCronTrigger(jobDetail, cronExpression);
    }

它在它的实现中使用this来保存对当前对象的引用。但是,当前对象当时也可能在创建中,从而导致此错误。我将重新注释将这2个文件移到config类。

此外,使用@dependson也不是一个好的做法。

 类似资料: