Java 与spring集成的Nullpointer异常
我在做一个SpringHibernate项目 我正在尝试连接到数据库并从quartzjob获取值。但是我得到了一个空指针异常。当我试图使用ApplicationContext对象并获取它连接到数据库的bean时,为什么我不能用另一种方法呢Java 与spring集成的Nullpointer异常,java,mysql,spring,hibernate,quartz-scheduler,Java,Mysql,Spring,Hibernate,Quartz Scheduler,我在做一个SpringHibernate项目 我正在尝试连接到数据库并从quartzjob获取值。但是我得到了一个空指针异常。当我试图使用ApplicationContext对象并获取它连接到数据库的bean时,为什么我不能用另一种方法呢 public class JobScheduler extends QuartzJobBean { @Autowired private SourceDaoImpl sourceDao; @Override protected
public class JobScheduler extends QuartzJobBean {
@Autowired
private SourceDaoImpl sourceDao;
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
Client client = new Client();
client.setClientKey(300);
sourceDao.getSourceByClient(client); **//error**
}
public SourceDaoImpl getSourceDao() {
return sourceDao;
}
public void setSourceDao(SourceDaoImpl sourceDao) {
this.sourceDao = sourceDao;
}
}
这是我的applicationcontext.xml
<!-- scheduler -->
<bean id="jobScheduler" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.dca.scheduling.JobScheduler" />
<property name="jobDataAsMap">
<map>
<entry key="timeout" value="5" />
</map>
</property>
</bean>
<bean id="cronTriggerjobScheduler" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobScheduler" />
<property name="cronExpression" value="0/10 * * * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="jobScheduler" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="cronTriggerjobScheduler" />
</list>
</property>
</bean>
<bean id="jobClass"
class="com.dca.scheduling.JobScheduler">
</bean>
hibernate.xml
<bean id="sourceInstanceDao" class="com.dca.dao.impl.SourceInstanceDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
不幸的是,答案很简单:sourceDao
为空
Quartz自己实例化作业类。向该类传递参数的唯一方法是使用executeInternal()
中的JobExecutionContext
首先,当您scheduleJob()
时,必须设置JobDetails
JobDetail thisJobDetail = new SimpleJobDetail<T>(this.getClass().getName(), (Class<? extends AbstractQuartzJob<T>>) this.getClass());
thisJobDetail.getJobDataMap().put(DATA_MANAGER_MAP_KEY,
dataManager);
sched.scheduleJob(thisJobDetail ,aTrigger);
在您的例子中,在这里使用sourceDao
作为dataManager
。Quartz在它自己的上下文中运行,即使它是由Spring便利方法启动的,因此您实际上不会得到整个Spring应用程序上下文,除非您显式地在JobDataMap中传递bean
<bean id="jobScheduler" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.vxl.appanalytix.dca.scheduling.JobScheduler" />
<property name="jobDataAsMap">
<map>
<entry key ="sourceDao" value-ref="sourceDao"/>
<entry key="timeout" value="5" />
</map>
</property>
</bean>
我没有看到连接dao类的配置。它在那里我没有显示它在hibernatecontex.xml中,我正在将该文件包括在Applicationcontext.xml中。请也发布该文件。我在hibernatecontext.xml中有这个文件。我想这就是问题所在。有什么解决方案吗
this.dataManager = (DataManager) jobContext.getJobDetail()
.getJobDataMap().get(DATA_MANAGER_MAP_KEY);
<bean id="jobScheduler" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.vxl.appanalytix.dca.scheduling.JobScheduler" />
<property name="jobDataAsMap">
<map>
<entry key ="sourceDao" value-ref="sourceDao"/>
<entry key="timeout" value="5" />
</map>
</property>
</bean>
protected void executeInternal(JobExecutionContext jobContext)
throws JobExecutionException {
Client client = new Client();
client.setClientKey(300);
sourceDao= (SourceDaoImpl) jobContext.getJobDetail()
.getJobDataMap().get("sourceDao");
}