title: ScheduledExecutorService problem with Autowired service
body: <p>I am trying to make a <code>ScheduledExecutor</code> service to run <code>MyTask</code> every day at certain time. The problem I have is that the task uses <code>@Service</code> component, and if I do <code>@Autowire</code> without <code>@Component</code>, the service won't work, if I put the <code>@Component</code> the <code>MyTask</code> runs first no matter the component where <code>Scheduler</code> is. </p>
  
  <p>I tried sending the service through constructor and <code>@DependsOn</code> but it didn't work. </p>
  
  <pre><code>@Component
  public class MyRunnable implements Runnable {
  
  @Autowired
  private Service service;
  
  @Override
  public void run() {
  
      //some code
  
      ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
  
      scheduler.scheduleAtFixedRate(new MyTask(),
          initalDelay,
          TimeUnit.DAYS.toSeconds(1),
          TimeUnit.SECONDS);
  
  
  }}
  </code></pre>
  
  <p>if <code>@Component</code> is put here, this runs first</p>
  
  <pre><code> public class MyTask extends TimerTask {
  
      @Autowired
      private Service service;
   }
  </code></pre>