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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章