我在一个非常基本的场景中遇到了一些麻烦:我有一个 java web 应用程序(spring 3),我想将一些对象存储到堆栈中。然后我想用调度程序管理堆栈中的对象,所以我实现了一个石英作业。干净利落。我将堆栈注入到第一个服务类和带有@Autowired 注释的作业类中。对于服务类,该表已成功注入,但对于作业类,该表为空。这是代码:
要分享的课程
package it.unifi.det.telemat.vr.data;
@Component
public class IndexedNodesStack extends HashMap<IndexedObject, Boolean>{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public synchronized int size() {
// TODO Auto-generated method stub
return super.size();
}
//all the HashMap methods are implemented in a synchronized wrapper method
}
第一堂课(@Autowired 成功)
package it.unifi.det.telemat.vr.service;
@Service
public class InnerNodeManager extends ConcreteNodeManager{
@Autowired
private IndexedNodesStack indexedNodesStack; //<--- it is actually autowired!
private void manageIndexedNodes(Boolean isPut, String lri, String features)
{
IndexedObject indexedObject = new IndexedObject();
indexedObject.setId(lri);
if(features != null && isPut)
indexedObject.generateFeatures(features);
indexedNodesStack.put(indexedObject, isPut);
}
}
作业类(@Autowired 失败)
package it.unifi.det.telemat.vr.service.scheduler;
@Component
public class QuartzJSearchJob extends QuartzJobBean{
@Autowired
private IndexedNodesStack indexedNodesStack; //<--- this variable is null :-(
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException
{
//do work
}
}
编辑:这是 servlet.xml
<context:component-scan base-package="it.unifi.det.telemat.vr" />
<bean name="searchJob"
class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="it.unifi.det.telemat.vr.service.scheduler.QuartzJSearchJob" />
</bean>
<bean id="searchJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="searchJob" />
<property name="cronExpression" value="0/50 * * * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="searchJobTrigger" />
</list>
</property>
</bean>
也许我对线程之间的资源共享缺乏一些了解,因为这是我在这个领域的第一次尝试。我错过了什么?