如何将Quartz Scheduler连接到Spring上下文中? [英] How to wire a Quartz Scheduler into my Spring context?
问题描述
我有一个应用程序,我想在其中使用Quartz Scheduler
对象。我已经阅读了关于此的Spring文档,他们建议使用 SchedulerFactoryBean
,如下所示:
I have an application in which I want to use a Quartz Scheduler
object. I've read the Spring documentation regarding this and they suggest to use a SchedulerFactoryBean
like this:
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="autoStartup">
<value>true</value>
</property>
<property name="configLocation" value="classpath:quartz.properties" />
</bean>
配置如下所示:
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
现在如果我想将 schedulerFactoryBean
注入我的一个对象中,我会得到一个例外说明:
Now if i want to inject schedulerFactoryBean
into one of my objects I get an exception stating:
无法将[org.quartz.impl.StdScheduler]类型的构造函数参数值转换为必需类型[org.springframework.scheduling.quartz.SchedulerFactoryBean]:
为什么我得到 StdScheduler
而不是 schedulerFactoryBean
?我是否错过了配置步骤?
Why do I get a StdScheduler
instead of a schedulerFactoryBean
? Do I miss a configuration step?
推荐答案
SchedulerFactoryBean
是 FactoryBean
所以它不能像普通的bean一样使用。当你将它注入其他bean时,Spring将注入工厂生成的 org.quartz.Scheduler
对象,它不会注入工厂本身。
A SchedulerFactoryBean
is a FactoryBean
so it can't be used like a normal bean. When you inject it into other beans, Spring will inject the org.quartz.Scheduler
object that the factory produces, it won't inject the factory itself.
通常将工厂bean命名为它生成的对象,因为它在引用它时会更好地读取。例如:
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 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 Scheduler
like this:
<bean id="beanThatNeedsScheduler" class="beanThatNeedsScheduler">
<!-- Will inject a Scheduler not a SchdulerFactoryBean -->
<property name="scheduler" ref="scheduler" />
</bean>
或使用注释:
@Component
public class BeanThatNeedsScheduler {
@Autowired;
private Scheduler scheduler
...
}
这篇关于如何将Quartz Scheduler连接到Spring上下文中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!