Java 弹簧靴&x2B;石英:“;请求的bean当前正在创建中:是否存在无法解析的循环引用;
在本教程()之后,我用Quartz scheduler在Spring Boot应用程序中实现了一个调度程序例程,并根据我的目的做了一些修改 例如,我的作业服务必须能够列出数据库中的所有对象及其子对象,设置新值并最终更新。所有这些都必须是事务性的 出于某种原因,MyJob类不允许在其中声明事务性方法,因此我通过使用transacitional方法注入一个新的服务类来解决这个问题 作业工作正常,但每次我运行应用程序时,它都会给我一些警告消息:Java 弹簧靴&x2B;石英:“;请求的bean当前正在创建中:是否存在无法解析的循环引用;,java,spring,spring-boot,quartz-scheduler,Java,Spring,Spring Boot,Quartz Scheduler,在本教程()之后,我用Quartz scheduler在Spring Boot应用程序中实现了一个调度程序例程,并根据我的目的做了一些修改 例如,我的作业服务必须能够列出数据库中的所有对象及其子对象,设置新值并最终更新。所有这些都必须是事务性的 出于某种原因,MyJob类不允许在其中声明事务性方法,因此我通过使用transacitional方法注入一个新的服务类来解决这个问题 作业工作正常,但每次我运行应用程序时,它都会给我一些警告消息: [main] WARN o.s.b.f.s.Defau
[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”)
我注意到作业班的自动布线服务导致了这种情况,但为什么呢?我要怎么做才能摆脱这些警告信息
欢迎提供如何在作业类中声明@Transacional
方法的解决方案
QuartzConfig.java
@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);
}
}
@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");
}
}
MyJob.java
@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);
}
}
@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");
}
}
MyService.java
@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);
}
}
@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");
}
}
看起来问题可能在<代码> CREATEJOBJORKEXION>代码>方法中,请考虑下面的代码片段:
@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);
}
它在保存当前对象引用的实现中使用此
。但是,当前对象当时也可能正在创建中,从而导致此错误。我建议将这2个类移动到Config
class
另外,使用
@DependsOn
不是一个好的做法。你说得对,移动这两个bean创建方法可以解决这个问题,但我是故意这么做的,因为我想将作业与配置隔离开来。