(1)工具类 SpringUtil.java
package com.googosoft.until; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class SpringUtil implements ApplicationContextAware { private static ApplicationContext appCtx; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { appCtx = applicationContext; } public static ApplicationContext getApplicationContext() { return appCtx; } public static Object getBean(String beanName) { return appCtx.getBean(beanName); } }
注:类上要加@Component注解,否则会报空指针
(2)使用
package com.googosoft.job; import javax.annotation.Resource; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import com.googosoft.service.QualityEvaluationService; import com.googosoft.until.SpringUtil; import com.googosoft.util.PageData; /** * @author songyan * @date 2020年3月31日 下午1:51:33 * @desc 作业 */ public class EvaluationPlanJob implements Job { @Resource private QualityEvaluationService service = (QualityEvaluationService) SpringUtil.getBean("qualityEvaluationService"); @Override public void execute(JobExecutionContext context) throws JobExecutionException { JobDataMap data = context.getJobDetail().getJobDataMap(); PageData pd = new PageData(); pd.put("GID", data.get("GID")); pd.put("DBMETAGID", data.get("DBMETAGID")); service.runEvaluationPlanList(pd); } }
(3)关键代码:
1)使用Resource注解
2)beanName:service类名首字母小写