Spring在第一次Quartz作业执行期间抛出IllegalStateException

时间:2015-07-24 07:36:09

标签: java spring quartz-scheduler

在Quartz调度程序中第一次执行第一个作业期间,Spring会抛出以下异常。请注意,作业在执行时会显式调用applicationContext.getBean(...)

有人可以解释这个例外的原因,也许是避免它的方法吗?

Spring版本:4.1.5.RELEASE 石英版:2.1.6

提前致谢

2015-07-24 09:20:27,416 ERROR be.citobi.mediquality.schedulers.A4MCubeJob - a4MCubeJob in error
java.lang.IllegalStateException: About-to-be-created singleton instance implicitly appeared through the creation of the factory bean that its bean definition points to
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:374)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1111)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1006)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:860)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:790)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:542)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:436)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:412)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:398)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:337)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968)
    at be.citobi.mediquality.schedulers.A4MCubeJob.execute(A4MCubeJob.java:26)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)

2 个答案:

答案 0 :(得分:0)

我不确定我的解决方案是否正确,但这就是我如何将Spring bean自动装配到我的工作中的方式。 SchedulerFactoryBean创建:

//Note: MySpringBean will be automatically autowired here. 
//Another possible approach is to use @Autowired and inject necessary bean one level higher
@Bean
public SchedulerFactoryBean scheduler(MySpringBean mySpringBean) {
    SchedulerFactoryBean sfb = new SchedulerFactoryBean();
    //default configuration goes here
    Map<String, Object> schedulerContext = new HashMap<>();
    schedulerContext.put("mySpringBean", mySpringBean);
    schedulerContext.setSchedulerContextAsMap(schedulerContext);
    return sfb;
}

这是我的Job代码,假设使用这个spring bean:

public class MyJob extends QuartzJobBean {
    private MySpringBean mySpringBean;

    public void setMySpringBean(MySpringBean mySpringBean) {
        this.mySpringBean = mySpringBean;
    }

    @Override
    public void executeInternal(JobExecutionContext jobContext) throws JobsExecutionException {
        //Job logic with usage of mySpringBean
    }
}

在我的应用程序中,Quartz作业不需要整个应用程序上下文 - 只需要3个bean,所以我决定自动装配某些bean而不是自动装配整个上下文

希望这有帮助

答案 1 :(得分:0)

我发现问题是什么,在Spring配置中,声明了几个实现FactoryBean的bean而没有使用泛型。添加泛型解决了这个问题。

这很可能与Quartz无关。