Announcement Announcement Module
Collapse
No announcement yet.
Extending SpringBeanJobFactory Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Extending SpringBeanJobFactory

    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

  • #2
    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 key

    Code:
    /**
     * @author Todd Nine
     * 
     */
    @Component
    public class AutowiredSpringBeanJobFactory extends SpringBeanJobFactory {
    
    	private SchedulerContext context;
    
    	private String applicationContextKey = "applicationContext";
    
    	@Override
    	protected Object createJobInstance(TriggerFiredBundle bundle)
    			throws Exception {
    
    		Assert.notNull(context, "Context must be set to use this object");
    		Assert.hasLength(applicationContextKey,
    				"You must set the application context key");
    
    		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;
    	}

    Comment


    • #3
      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.

      Comment

      Working...
      X