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

在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);
即可

发布了15 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Himly_Zhang/article/details/59112721