Java springmvc与Quartz集成
我想用Quartz调度器替换Spring MVC webapp中的TimerTask。 My spring-quartz.xml:Java springmvc与Quartz集成,java,spring,quartz-scheduler,Java,Spring,Quartz Scheduler,我想用Quartz调度器替换Spring MVC webapp中的TimerTask。 My spring-quartz.xml: <bean id="manifestTask" class="it.dhl.wla.quartz.ManifestTask" /> <bean id="manifestJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<bean id="manifestTask" class="it.dhl.wla.quartz.ManifestTask" />
<bean id="manifestJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="manifestTask" />
<property name="targetMethod" value="go" />
</bean>
<bean id="manifestTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="manifestJob" />
<property name="cronExpression" value="59 15 * * * ?" />
</bean>
<!-- MANIFEST -->
<bean id="theManifestService" class="it.dhl.wla.service.ManifestService"/>
<bean id="manifestTask" class="it.dhl.wla.quartz.ManifestTask" >
<property name="manifestService" ref="theManifestService"/>
</bean>
<bean id="manifestJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="manifestTask" />
<property name="targetMethod" value="go" />
</bean>
<bean id="manifestTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="manifestJob" />
<property name="cronExpression" value="59 15 * * * ?" />
</bean>
这是启动时的错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [it.dhl.wla.service.ManifestService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
ManifestService在其他类中自动连接并工作,在quartz任务的这个特定案例中,bean实例化顺序似乎有问题
弹簧版为3.0.5,石英版为1.8.6
编辑:
在ManifestTask中添加了对ApplicationContext的引用:
@Autowired
ApplicationContext appctx=null;
好的,当quartz调用方法go()时,我研究了appctx,但找不到任何用注释声明的@Components。相反,我找到了spring-quartz.xml中声明的bean
appctx的具体类是XmlWebApplicationContext。
xml声明的bean是否在一个单独的上下文中?如何访问带注释的@Components?您应该使用注释您的
ManifestTask
,以便spring:D能够获取该任务
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ManifestTask {
@Autowired
ManifestService manifestService;
private static final Logger LOG = Logger.getLogger(ManifestTask.class);
public void go() {
LOG.info("Manifest quartz task start...");
boolean res=manifestService.doManifest();
LOG.info("Manifest quartz task end: "+res);
}
}
同样在看了你编辑的帖子后,我认为你应该读一下这个答案:我找到了一个解决办法,但我不喜欢 spring-quartz.xml:
<bean id="manifestTask" class="it.dhl.wla.quartz.ManifestTask" />
<bean id="manifestJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="manifestTask" />
<property name="targetMethod" value="go" />
</bean>
<bean id="manifestTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="manifestJob" />
<property name="cronExpression" value="59 15 * * * ?" />
</bean>
<!-- MANIFEST -->
<bean id="theManifestService" class="it.dhl.wla.service.ManifestService"/>
<bean id="manifestTask" class="it.dhl.wla.quartz.ManifestTask" >
<property name="manifestService" ref="theManifestService"/>
</bean>
<bean id="manifestJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="manifestTask" />
<property name="targetMethod" value="go" />
</bean>
<bean id="manifestTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="manifestJob" />
<property name="cronExpression" value="59 15 * * * ?" />
</bean>
现在,ManifestService bean和依赖项是用xml定义的。
但是我的ManifestService类仍然被注释为@Component,因为我在一些控制器中使用它
我想现在我有了两个独立的ManifestService实例…按照以下步骤将Quartz与Spring boot集成 1-在pom.xml文件中添加以下依赖项—它还需要spring上下文支持
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
如果仍然有一些疑问,然后点击下面的链接,看看现场演示
我怀疑发生的是,
ManifestService
bean被扫描并实例化在其中一个xxxx-servlet.xml Spring内容文件中。在yyy-servlet.xml中处理/实例化bean时,以这种方式创建的bean不可用
要使bean对所有Spring上下文全局可用,您需要扫描它所属的包,或者在与Spring上下文加载程序侦听器关联的applicationContext.xml文件中定义它。比如:
web.xml
...
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
...
。。。
上下文配置位置
/WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
...
缺少的bean是ManifestService,它是一个@component,您是对的。所以,如果这个bean丢失了,并且用@Component注释了它,那么它可能没有被spring配置扫描?是否正确?组件已扫描,因为我已将其自动连接到某些控制器中。看看我对这个问题的编辑……您是否有不止一个spring配置xml?除了spring-quartz.xml之外,您还有其他spring配置吗?如果需要,您需要在主配置xml中使用,以便两者都可以使用。我在web.xml:contextConfigLocation类路径:applicationContext.xml类路径:applicationContext-security.xml类路径:spring-quartz.xml中定义了一些xml。我的问题是带注释的组件。。。
import java.util.Date;
public class ExecuteUsingQuartz {
public void run() {
System.out.println("Executing using quartz in every 15 seconds " + new Date());
}
}
...
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
...