I want to substitute a TimerTask in a Spring MVC webapp with a Quartz scheduler. My 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>
It works with an empty go() method (with just a log output) The problem is with a ManifstTask dependence that i try to @Autowire:
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);
}
}
This is the error on startup:
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 is autowired in other classes and works, seems a problem with bean instantiation order in this specific case of quartz task.
Spring version is 3.0.5, Quartz 1.8.6.
EDIT:
Added a reference to ApplicationContext in ManifestTask :
@Autowired
ApplicationContext appctx=null;
Well, when method go() is called by quartz I explore the appctx but can't find any of the @Components declared with annotation. I find instead the beans declared in spring-quartz.xml.
Concrete class for appctx is XmlWebApplicationContext. Are xml-declared beans in a separated context? How can I access to annotated @Components?