How to use @Autowired in a Quartz Job?

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

    Not sure if this is what you want, but you can pass some configuration values to the Quartz job. I believe in your case you could take advantage of the jobDataAsMap property you already set up, e.g.:

     
        
          
          
         
      
    

    Then you should be able to access it in your actual Java code in manual way:

    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        schedulerTask.printSchedulerMessage();
        System.out.println(context.getJobDetail().getJobDataMap().getString("param1"));
    }
    

    Or using the magic Spring approach - have the param1 property defined with getter/setter. You could try defining it with java.lang.Class type then and have the done automatically (Spring would do it for you):

     private Class param1;
    
     // getter & setter
    
     protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        schedulerTask.printSchedulerMessage();
        System.out.println("Class injected" + getParam1().getName());
     }     
    

    I haven't tested it though.

提交回复
热议问题