您的位置:首页 > 编程语言 > Java开发

Spring+Quartz 定时任务无法自动注入bean的问题

2017-03-01 17:33 696 查看
在applicationcontext.xml中配置了Quartz 用来管理代码中的定时任务……但是问题出现了,serverce和dao均无法注入:

xml中配置方法

<bean id="jobAuditCompile" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.easternie.ylfsh.service.aotuaudit.AutoAuditService"/>
</bean>
<bean id="triggerAuditCompile" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="jobAuditCompile"/>
<property name="startDelay" value="5000"/>
<property name="repeatInterval" value="60000"/>
</bean>


最后的解决办法

@Service("autoService")
public class AutoAuditService implements Job{
@Autowired
private Md01Mapper md01Mapper;
@Autowired
private Md05Mapper md05Mapper;
@Autowired
private Md05RuleMapper md05RuleMapper;

public static void main(String[] args) throws InstantiationException, IllegalAccessException {
// AutoAudit au = new AutoAudit();
// au.auditOne();
}

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
System.out.println("自动执行审核");
try {
this.auditOne();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


在execute方法中直接增加

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: