• 工具类中使用service


    (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类名首字母小写

  • 相关阅读:
    Python模块:struct
    Python模块:hashlib
    Python模块:collections
    python:爬虫
    Python:进程
    Python:线程
    Python:socket
    Welcome to ARFA's cnblog ! (Click me)
    骗访问量的机房人物列传by xMinh
    关于我
  • 原文地址:https://www.cnblogs.com/excellencesy/p/12624322.html
Copyright © 2020-2023  润新知