title: Using @Autowired service in quartz job
body: <p>I'm using spring boot and I have a service which stores/retrieves some data from DB. I want to use a quartz job which uses my service. I tried many suggestions from the internet to integrate quartz with spring boot, but it doesn't work. Can you help me pls?<br>
  Here is my code:<br>
  1) I added org.quartz-scheduler as dependency in pom.xml:<br></p>
  
  <pre><code>&lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;mysql&lt;/groupId&gt;
          &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
  
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.quartz-scheduler&lt;/groupId&gt;
          &lt;artifactId&gt;quartz&lt;/artifactId&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>2) This is my application.properties configured to use mysql as DB for my service:<br></p>
  
  <pre>
  
      server.port=8281
  
      spring.datasource.driverClassName=com.mysql.jdbc.Driver
      spring.datasource.url=jdbc:mysql://localhost:3306/mydb
      spring.datasource.username=myusername
      spring.datasource.password=mypassword
  
  </pre>
  
  <p>3) This is my service:<br></p>
  
  <pre>
  
      @Service
      public class ReportService {
  
          JdbcTemplate jdbcTemplate;
  
          public ReportService(JdbcTemplate jdbcTemplate) {
              this.jdbcTemplate = jdbcTemplate;
          }
  
          public List getPendingReports() {
              String sql = "SELECT * FROM report WHERE status = '" + ReportStatus.PENDING.name() +"'";
              ReportMapper reportsMapper = new ReportMapper();
              List reports = jdbcTemplate.query(sql, reportsMapper);
              return reports;
          }
      }
  
  </pre>
  
  <p>4) The scheduler class:</p>
  
  <pre>
  
      import org.quartz.JobBuilder;
      import org.quartz.JobDetail;
      import org.quartz.Scheduler;
      import org.quartz.SchedulerException;
      import org.quartz.SchedulerFactory;
      import org.quartz.SimpleScheduleBuilder;
      import org.quartz.SimpleTrigger;
      import org.quartz.TriggerBuilder;
      import org.quartz.impl.StdSchedulerFactory;
  
      public class ReportScheduler {
  
          public void scanAndUpdateReports() {
              SchedulerFactory sf = new StdSchedulerFactory();
              try {
                  Scheduler scheduler = sf.getScheduler();
                  scheduler.start();
  
                  JobDetail job = JobBuilder.newJob(ReportsJob.class)
                            .withIdentity("reportsJob")
                            .build();
  
                  SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger()
                            .withIdentity("reportsTrigger")
                            .startNow()
                            .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                                    .withIntervalInSeconds(60).repeatForever())
                            .build();
  
                  scheduler.scheduleJob(job, trigger);
              } catch (SchedulerException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
  
  </pre>
  
  <p>5) And my job class where I want to autowire the ReportService, but it is null:</p>
  
  <pre>
  
      public class ReportsJob implements Job {
  
          @Autowired
          ReportService reportService; //this doesn't work (it is null) 
  
          @Override
          public void execute(JobExecutionContext context) throws JobExecutionException {
              List reports = reportService.getPendingReports();
              System.out.println("Reports:\n"+reports);
          }
      }
  
  </pre>
  
  <p>6) Right now I'm calling the scheduler from main method:<br></p>
  
  <pre>
  
      @SpringBootApplication
      public class ReportAppBeApplication {
  
          public static void main(String[] args) {
              SpringApplication.run(ReportAppBeApplication.class, args);
  
              ReportScheduler rs = new ReportScheduler();
              rs.scanAndUpdateReports();
          }
  
      }
  
  </pre>