title: How to inject Spring Bean in thread
body: <p>I have a running Spring 3 web application. All the beans are correctly injected and everything is working as it should (all web service calls are working properly).</p>
  
  <p>While expanding the application I needed to add threads that can be started &amp; stopped via a web service.</p>
  
  <p>In the thread I need to inject some Spring beans. These beens are services (annotated with @Service). In my applicationContext the beans are detected via a component scan:</p>
  
  <pre><code>&lt;context:component-scan base-package="&lt;package&gt;"&gt;
      &lt;context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /&gt;
  &lt;/context:component-scan&gt;
  </code></pre>
  
  <p>But when I try to inject the beans (using @Resource) in the thread they are always 'null' (Spring doesn't inject them). The thread is started but fails while initializing.</p>
  
  <p>I also tried injecting them by loading the applicationContext in code: (application context is located in 'src/main/resources')</p>
  
  <pre><code>ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:*applicationContext.xml");
  
  if(applicationContext.containsBean("BeanName")) {
  
          beanObject = (BeanClass) applicationContext.getBean("BeanName");
  
  } else {
  
      //Exception
  }
  </code></pre>
  
  <p>Does anyone have any idea how to inject the beans in the thread? Or isn't it possible to inject beans in a thread?</p>
  
  <p>Thanks in Advance!</p>