ServletContext Autowire为空

时间:2018-08-09 12:09:35

标签: java spring spring-mvc

我正在使用下面的代码来解决自动接线问题。在ProductLoaderJob的下面的代码中,我将SC设置为null。任何人都可以提出问题的答案。 为什么我会变空。

@Controller
public class ProductLoaderJob implements Job,ServletContextAware
{

    @Autowired
    private ServletContext sc;

    @Autowired
    IBasketService basketService;   

    @Autowired
    JwtTokenValidator jwtval;

    private static final Logger log = LogManager.getLogger(ProductLoaderJob.class);
    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        log.info("ProductLoaderJob@execute : Loading productdata..");

        if(basketService == null)
            log.info("jwt is null");
        if(sc == null)
            log.info("SC is null");
        PTWebContext ptweb = (PTWebContext) sc.getAttribute(WBUtils.PTWEB_CONTEXT);
        List<String> ricFromAllRegionList = new ArrayList<String>();
        Enumeration regions = ptweb.getRegionNames();
        List<String> regionList = Collections.list(regions);
        try{
            for(String region:regionList){
               log.info(String.format("ProductLoaderJob@execute : Loading productdata for region : [%s]",region));  
               List<String> riclList = ptweb.getRegion(region).getBrokerManager().getRicFromBaskets();
               log.info(String.format("ProductLoaderJob@execute : Loaded productdata for region : [%s] size:[%d]",region,riclList.size())); 
               if(!riclList.isEmpty())
                   ricFromAllRegionList.addAll(riclList);
            }
        }catch(PTWebException ex){
            log.error("ProductLoaderJob@execute Error while retriving rics",ex);

        }
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        // TODO Auto-generated method stub
        this.sc = servletContext;
    }

}

@Component
public class PtCronJobScheduler {

    @Autowired
    ProductLoaderJob productJob;

    @Autowired
    ServletContext sc;

    private String jobName;
    private Job job;
    private String cronExpression;

    private static final Logger log = LogManager.getLogger(PtCronJobScheduler.class);
    public String getJobName() {
        return jobName;
    }
    public void setJobName(String jobName) {
        this.jobName = jobName;
    }

    public Job getJob() {
        return job;
    }
    public void setJob(Job job) {
        this.job = job;
    }
    public String getCronExpression() {
        return cronExpression;
    }
    public void setCronExpression(String cronExpression) {
        this.cronExpression = cronExpression;
    }   

    public void schedule() {

        try{
            JobDetail jobDetail = new JobDetail();
            jobDetail.setName("ProductLoad");
            setJob(productJob);
            jobDetail.setJobClass(job.getClass());

            if(sc == null)
                log.info("OMG IS NULl");

            CronTrigger trigger = new CronTrigger();
            trigger.setName("PtJobTrigger");
            trigger.setCronExpression("0 0/2 6-20 ? * SUN,MON,TUE,WED,THU,FRI");

            Scheduler scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.start();
            scheduler.scheduleJob(jobDetail, trigger);
        }catch(Exception ex){
            log.error("PtCronJobScheduler@schedule : Error while scheduling product loader job.",ex);
        }


    }


}

0 个答案:

没有答案