How to use @Autowired in a Quartz Job?

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

    In your solution you are using the spring @Autowired annotation in a class that is not instantiated by Spring. Your solution will still work if you remove the @Autowired annotation because Quartz is setting the property, not Spring.

    Quartz will try to set every key within the JobDataMap as a property. E.g. since you have a key "myDao" Quartz will look for a method called "setMyDao" and pass the key's value into that method.

    If you want Spring to inject spring beans into your jobs, create a SpringBeanJobFactory and set this into your SchedulerFactoryBean with the jobFactory property within your spring context.

    SpringBeanJobFactory javadoc:

    Applies scheduler context, job data map and trigger data map entries as bean property values

提交回复
热议问题