How to use @Autowired in a Quartz Job?

前端 未结 5 2034
余生分开走
余生分开走 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:40

    As mentioned in inject bean reference into a Quartz job in Spring? you can use spring SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

    @Named
    public class SampleJob implements Job {
    
        @Inject
        private AService aService;
    
       @Override
        public void execute(JobExecutionContext context)
            throws JobExecutionException {
    
           //Do injection with spring
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
            aService.doIt();
           }
    }
    

    As mentioned it may not wotk on some spring version but I have tested it on 4.2.1.RELEASE which worked fine.

提交回复
热议问题