title: Why does Autowiring not function in a thread?
body: <p>I've made a maven project in Spring 3.0, I've made some DAO, services and controllers, in one of mine controller I call a service in which I start a thread, the problem is that in the thread I declare a "service variable" that should be initialized with Autowired annotiation, but it doesn't work and the variable isn't initilized and has the value null.</p>
  
  <p>this is the thread class</p>
  
  <pre><code>package com.project.tasks;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.core.task.TaskExecutor;
  import org.springframework.stereotype.Component;
  
  import com.project.entities.user.User;
  import com.project.services.IUserService;
  
  @Component
  public class AddFriendInMyFriendListTaskExecutor {
  private class AddFriendInMyFriendListTask implements Runnable {
  
  
       // HERE IS THE PROBLEM
      @Autowired
      private IUserService uService;
  
      private User a;
      private User b;
  
      public AddFriendInMyFriendListTask() {
          ;
      }
  
      public AddFriendInMyFriendListTask(User aA, User bB) {
          a = aA;
          b = bB;
      }
  
      public User getA() {
          return a;
      }
      public void setA(User a) {
          this.a = a;
      }
      public User getB() {
          return b;
      }
      public void setB(User b) {
          this.b = b;
      }
  
  
      public void run() {
                      // FROM HERE IT PRINTS THE VALUE OF uService THAT IS NULL
          System.out.println("uService:" + uService);
          uService.insertRightUserIntoLeftUserListOfFriends(a, b);
      }
  }
  
  private TaskExecutor taskExecutor;
  
    public AddFriendInMyFriendListTaskExecutor(TaskExecutor taskExecutor) {
      this.taskExecutor = taskExecutor;
    }
  
    public void doIt(User a, User b) {
        taskExecutor.execute(new AddFriendInMyFriendListTask(a, b));
    }
  }
  </code></pre>
  
  <p>this is the piece of code that calls the thread</p>
  
  <pre><code>    User a = uDao.getUser(hrA.getMyIdApp());
      User b = uDao.getUser(hrA.getOtherIdApp());
      SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
      AddFriendInMyFriendListTaskExecutor tmp = new AddFriendInMyFriendListTaskExecutor(taskExecutor);
      tmp.doIt(a, b);
  </code></pre>
  
  <p>I'd like to highlight that in all the other tests in which I don't call any threads, the Autowired of a UserService instance functions correctly!
  The method I call: insertRightUserIntoLeftUserListOfFriends(User a, User b), works correctly.</p>