针对autowire的当前环境下的弹簧注入不起作用?

时间:2014-04-15 10:09:26

标签: java spring spring-mvc quartz-scheduler autowired

当我在Quartz工作中使用autowire时,我遇到了问题。我看到问题是因为Quartz而不是Spring创建的作业,我使用过SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);在我工作的第一行执行。 奇怪的是,它在我的localhost中运行良好,但是当我在服务器中部署它时它不会自动装配并且注入无法完成,因此服务为空,我收到了此消息

"Current WebApplicationContext is not available for processing of
MeterReadingJob: Make sure this class gets constructed in a Spring web
application. Proceeding without injection."

来自SpringBeanAutowiringSupport。

我已经在我的localhost和服务器之间匹配了java和tomcat,但它仍然会抛出相同的错误。

这是我的ApplicationListener

@Component
public class QuartzInitializerListener implements ApplicationListener<ContextRefreshedEvent>
{
    @Autowired
    private JobService jobService;

    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("Starting The Event :"+event.getApplicationContext().getDisplayName());
        if(event.getApplicationContext().getParent() == null){
            jobService.meterReadingJob();
        }
    }


}

这是MeterReadingJob类:

@Component
public class MeterReadingJob implements Job  {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired(required = true)
    private TransactionService trxSvc; //This one doesnt autowired correctly

    public void execute(JobExecutionContext jec) throws JobExecutionException {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); //work on localhost only..

        MapTbl mapTbl = (MapTbl)jec.getJobDetail().getJobDataMap().get("mapTbl");
        System.out.println("SERVICE: "+trxSvc);
        String trxId = trxSvc.validTxIdWithS(4);
        ......
    }

这是侦听器的web.xml

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener>

这是我用于抄表作业的调度程序服务方法

/*
     * Meter reading schedule for all mapped meter
     */
    public void meterReadingJob() {
        List<MapTbl> mapTbls = mapTblDao.findAll();
        try {
            if(scheduler.isShutdown()){
                scheduler = StdSchedulerFactory.getDefaultScheduler();
            }
            for(MapTbl mapTbl : mapTbls){       
                if(mapTbl.getInterval()!=0){
                    JobDetail job = JobBuilder.newJob(MeterReadingJob.class)
                            .withIdentity( mapTbl.getMapId().toString(), "MeterReading").build();

                    job.getJobDataMap().put("mapTbl", mapTbl);

                    Trigger trigger = TriggerBuilder.newTrigger()
                            .withIdentity( mapTbl.getMapId().toString(), "MeterReading")
                            .withSchedule(
                                SimpleScheduleBuilder.simpleSchedule()
                                    .withIntervalInMilliseconds(mapTbl.getInterval())
                                    .repeatForever())
                            .startNow()
                            .build();
                        scheduler.scheduleJob(job, trigger);
                }
            }
            if(mapTbls !=null && mapTbls.size()>0 && !scheduler.isStarted()){
                scheduler.start();
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
        }



    }

它在我的localhost中运行顺利,但在我的服务器中,服务始终为null [not autowired]

有什么我错过的,所以它不会在我的服务器上运行? 我正在使用apache tomcat 7.0.42 jdk 7 springframework 3.2.0

0 个答案:

没有答案