Java 如何将 Quartz Scheduler 连接到我的 Spring 上下文中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21557066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:22:08  来源:igfitidea点击:

How to wire a Quartz Scheduler into my Spring context?

javaspringquartz-scheduler

提问by Adam Arold

I have an application in which I want to use a Quartz Schedulerobject. I've read the Spring documentation regarding this and they suggest to use a SchedulerFactoryBeanlike this:

我有一个应用程序,我想在其中使用 QuartzScheduler对象。我已经阅读了关于这个的 Spring 文档,他们建议使用SchedulerFactoryBean这样的:

<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="autoStartup">
        <value>true</value>
    </property>
    <property name="configLocation" value="classpath:quartz.properties" />
</bean>

The config looks like this:

配置如下所示:

org.quartz.scheduler.skipUpdateCheck = true
org.quartz.scheduler.instanceName = MyQuartzScheduler
org.quartz.scheduler.jobFactory.class = org.quartz.simpl.SimpleJobFactory
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 5
log4j.rootLogger=INFO, stdout
log4j.logger.org.quartz=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

Now if i want to inject schedulerFactoryBeaninto one of my objects I get an exception stating:

现在,如果我想注入schedulerFactoryBean我的一个对象,我会收到一个异常说明:

Could not convert constructor argument value of type [org.quartz.impl.StdScheduler] to required type [org.springframework.scheduling.quartz.SchedulerFactoryBean]:

Could not convert constructor argument value of type [org.quartz.impl.StdScheduler] to required type [org.springframework.scheduling.quartz.SchedulerFactoryBean]:

Why do I get a StdSchedulerinstead of a schedulerFactoryBean? Do I miss a configuration step?

为什么我得到一个StdScheduler而不是一个schedulerFactoryBean?我错过了一个配置步骤吗?

采纳答案by Will Keeling

A SchedulerFactoryBeanis a FactoryBeanso it can't be used like a normal bean. When you inject it into other beans, Spring will inject the org.quartz.Schedulerobject that the factory produces, it won't inject the factory itself.

ASchedulerFactoryBean是 aFactoryBean所以它不能像普通 bean 一样使用。当你将它注入到其他 bean 中时,Spring 会注入org.quartz.Scheduler工厂生产的对象,它不会注入工厂本身。

It is common to name the factory bean after the object that it produces, as it reads better when you're referencing it. For example:

以它产生的对象命名工厂 bean 是很常见的,因为当你引用它时它会更好地阅读。例如:

<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="autoStartup">
        <value>true</value>
    </property>
    <property name="configLocation" value="classpath:quartz.properties" />
</bean>

Then you can configure an object that needs a Schedulerlike this:

然后你可以配置一个需要Scheduler这样的对象:

<bean id="beanThatNeedsScheduler" class="beanThatNeedsScheduler">
    <!-- Will inject a Scheduler not a SchdulerFactoryBean -->
    <property name="scheduler" ref="scheduler" />
</bean>

Or using annotations:

或者使用注释:

@Component
public class BeanThatNeedsScheduler {

    @Autowired;
    private Scheduler scheduler

    ...
}

回答by bearlee

SchedulerFactoryBean will creates and configures a org.quartz.Quartz,manages its lifecycle as part of the Spring application context, and exposes the Scheduler as bean reference for dependency injection.

SchedulerFactoryBean 将创建和配置一个 org.quartz.Quartz,将其生命周期作为 Spring 应用程序上下文的一部分进行管理,并将 Scheduler 作为 bean 引用公开以进行依赖注入。

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">...</bean>

and you can

你可以

@Component
public class YourTask {

@Inject
private Scheduler scheduler

}