title: Spring @Autowired OK on JUnit, NPE on main class
body: <p>I have a open source project hosted at <a href="https://github.com/robertobado/open-restaurant-core" rel="nofollow">this</a> github. I am facing a weird scenario. I found many people who could make @Autowired Spring annotation work on their main classes but not on their JUnit test classes. My problem is the other way around. I can successfully use @Autowired in my JUnit test class but when a test calls my main class, the dependencies don't get injected there. Here is my context (simplified version):</p>
  
  <p>Login Class:</p>
  
  <pre><code>package net.openrally.restaurant.core.exposure.resource;
  
  @Path("/login")
  @Component
  @Transactional
  @Singleton
  @Produces("application/json")
  @Consumes("application/json")
  public class Login extends BaseResource{
  
      @Autowired
      private UserDAO userDAO;
  
      @POST
      public Response post(String requestBody){
  
          ...
  
          //NullPointerException
          User user = userDAO.loadByCompanyIdAndLogin(companyId, login);
      }
  
      ...
  
  }
  </code></pre>
  
  <p>LoginTest Class:</p>
  
  <pre><code>package net.openrally.restaurant.core.exposure.resource;
  
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration("/applicationContext.xml")
  public class LoginTest extends BaseResourceTest {
  
      @Autowired
      private UserDAO userDAO;
  
      ...
  
      @Test
      public void testInvalidPassword() {
  
      ....
  
      // Works perfectly!
      userDAO.save(user);
  
      ....
  
      }
  
  }
  </code></pre>
  
  <p>applicationContext.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
          &lt;property name="locations"&gt;
              &lt;list&gt;
                  &lt;value&gt;classpath:configuration.properties&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;context:annotation-config /&gt;
      &lt;context:component-scan base-package="net.openrally.restaurant.core" /&gt;
  
      &lt;import resource="db-config.xml" /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>I am searching for a solution for a few days now. From what I could find so far, most of the people who have <code>@Autowired</code> problems dont have either <code>&lt;context:annotation-config /&gt;</code>  or <code>xmlns:context="http://www.springframework.org/schema/context"</code> in their spring xml or dont have a <code>@Component</code> family annotation in the class they want DI to take place, and as you can see, they are both there :(. I have only one <code>applicationContext.xml</code> in my project which suits for both runtime and test (I have distinct configuration.properties to set database credentials and log levels differently, but no spring configuration there)</p>
  
  <p>I'm using:</p>
  
  <pre><code>Spring: 3.1.0.RELEASE
  JUnit: 4.10
  Jersey: 1.11
  CLIB: 2.2.2
  </code></pre>
  
  <p>Any ideas, and I mean ANY :), is much appreciated.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The following logs come up when I run the test:</p>
  
  <pre><code>2012-03-27 07:37:02,457 DEBUG [main] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'login'
  2012-03-27 07:37:02,457 DEBUG [main] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'login'
  2012-03-27 07:37:02,459 DEBUG [main] org.springframework.beans.factory.annotation.InjectionMetadata - Found injected element on class [net.openrally.restaurant.core.exposure.resource.Login]: AutowiredFieldElement for private net.openrally.restaurant.core.persistence.dao.ConfigurationDAO net.openrally.restaurant.core.exposure.resource.Login.configurationDAO
  2012-03-27 07:37:02,459 DEBUG [main] org.springframework.beans.factory.annotation.InjectionMetadata - Found injected element on class [net.openrally.restaurant.core.exposure.resource.Login]: AutowiredFieldElement for private net.openrally.restaurant.core.persistence.dao.UserDAO net.openrally.restaurant.core.exposure.resource.Login.userDAO
  2012-03-27 07:37:02,459 DEBUG [main] org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'login' to allow for resolving potential circular references
  2012-03-27 07:37:02,459 TRACE [main] org.springframework.beans.CachedIntrospectionResults - Getting BeanInfo for class [net.openrally.restaurant.core.exposure.resource.Login]
  2012-03-27 07:37:02,462 TRACE [main] org.springframework.beans.CachedIntrospectionResults - Caching PropertyDescriptors for class [net.openrally.restaurant.core.exposure.resource.Login]
  2012-03-27 07:37:02,462 TRACE [main] org.springframework.beans.CachedIntrospectionResults - Found bean property 'class' of type [java.lang.Class]
  2012-03-27 07:37:02,462 DEBUG [main] org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected method of bean 'login': AutowiredFieldElement for private net.openrally.restaurant.core.persistence.dao.ConfigurationDAO net.openrally.restaurant.core.exposure.resource.Login.configurationDAO
  2012-03-27 07:37:02,462 DEBUG [main] org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'configurationDAO'
  2012-03-27 07:37:02,462 DEBUG [main] org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - Autowiring by type from bean name 'login' to bean named 'configurationDAO'
  2012-03-27 07:37:02,463 DEBUG [main] org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected method of bean 'login': AutowiredFieldElement for private net.openrally.restaurant.core.persistence.dao.UserDAO net.openrally.restaurant.core.exposure.resource.Login.userDAO
  2012-03-27 07:37:02,463 DEBUG [main] org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'userDAO'
  2012-03-27 07:37:02,463 DEBUG [main] org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - Autowiring by type from bean name 'login' to bean named 'userDAO'
  2012-03-27 07:37:02,464 DEBUG [main] org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator - Creating implicit proxy for bean 'login' with 0 common interceptors and 1 specific interceptors
  2012-03-27 07:37:02,464 DEBUG [main] org.springframework.aop.framework.Cglib2AopProxy - Creating CGLIB2 proxy: target source is SingletonTargetSource for target object [net.openrally.restaurant.core.exposure.resource.Login@60532a0a]
  2012-03-27 07:37:02,465 DEBUG [main] org.springframework.aop.framework.Cglib2AopProxy - Unable to apply any optimisations to advised method: public javax.ws.rs.core.Response net.openrally.restaurant.core.exposure.resource.Login.post(java.lang.String)
  </code></pre>
  
  <p>Could Spring be creating my bean correctly but Jersey using its own non-autowired instance?</p>