quartzy的spring注入问题
<!-- quartz定时器,定时器的类--自己用的类 --> <bean id="jobDetailFactoryBean" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="com.myk.oe.utils.QuartzTask"></property> </bean> <!-- 配置执行时间和规则 --> <bean id="quartzTask" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="jobDetailFactoryBean"></property> <!-- 5秒一次 --> <property name="cronExpression" value="0/5 * * * * ?"></property> </bean> <!-- 将bean注入到定时器的类 --> <bean id="jobFactory" class="com.myk.oe.quartz.util.JobFactory"></bean> <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false"> <property name="triggers"> <list> <ref bean="quartzTask" /><!-- 任务 --> </list> </property> <property name="jobFactory" ref="jobFactory"></property> <property name="autoStartup" value="true"/><!-- 自动开始 --> </bean>
定义好定时任务的相关类,在类中调用dao或者service的时候会报空指针异常,
在定时任务的类上加注解@DisallowConcurrentExecution就可以注入service和dao了
@DisallowConcurrentExecution public class QuartzTask extends QuartzJobBean{ @Resource(name="appointmentServiceImpl") private AppointmentService appointmentService;//这是服务类 @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { List<Appointment> appointments = appointmentService.getAppointments();//获取数据 System.err.println(appointments); } }