How to use @Autowired in a Quartz Job?

前端 未结 5 2036
余生分开走
余生分开走 2020-12-25 11:52

i am using quartz with spring and i want to inject/use another class in the job class and i don\'t know how to do it correctly

the xml:



        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-25 12:32

    this is my solution:

        public class MySpringBeanJobFactory extends
            org.springframework.scheduling.quartz.SpringBeanJobFactory implements
            ApplicationContextAware {
          private ApplicationContext ctx;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext)
                throws BeansException {
            this.ctx = applicationContext;
        }
    
    
        @Override
        protected Object createJobInstance(TriggerFiredBundle bundle)
                throws Exception {
    
            Object jobInstance = super.createJobInstance(bundle);
            ctx.getAutowireCapableBeanFactory().autowireBean(jobInstance);
            return jobInstance;
        }
    }
    

    then config the class of MySpringBeanJobFactory in the xml:

    
            
                
        
            
            
                
                    
                
            
        
    

    Good luck ! :)

提交回复
热议问题