Hi all, I want to create an extension of the SpringBeanJobFactory that performs annotation scanning and auto wires dependencies from an application context. Which class in the framework do I use to auto-scan an instance for spring annotations and inject them using an existing application context? All of my quartz jobs are POJO beans which simply delegate variables set in the context to a spring service. Rather than have to manually get retrieve each object from the applicationcontext, I would prefer to have jobs autowired using the spring annotations when Quartz instanciates them.
Thanks,
Todd
Here's an example class for anyone else looking for this answer. Note that this assumes the application context has been set into the scheduler context with the given keyCode:
/*** @author Todd Nine* */
@Component
public class AutowiredSpringBeanJobFactory extends SpringBeanJobFactory {
private SchedulerContext context;
private String applicationContextKey = quot;applicationContextquot;;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle)
throws Exception {
Assert.notNull(context, quot;Context must be set to use this objectquot;);
Assert.hasLength(applicationContextKey,
quot;You must set the application context keyquot;);
Object instance = super.createJobInstance(bundle);
ApplicationContext context = (ApplicationContext) this.context.get(applicationContextKey);
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(context.getAutowireCapableBeanFactory());
bpp.processInjection(instance);
return instance;
}@Override
public void setSchedulerContext(SchedulerContext schedulerContext) {
super.setSchedulerContext(schedulerContext);
this.context = schedulerContext;
}
public void setApplicationContextKey(String applicationContextKey) {
this.applicationContextKey = applicationContextKey;
}public String getApplicationContextKey() {
return applicationContextKey;
}how do you configure this? I do not understand why there is no DI when using Quartz. Why use Spring with Quartz at all? It adds nothing unless DI is handled for you by the framework. |