title: Unable to get Autowired to work or do a component scan
body: <p>I'm using Spring for the first time and even though I feel like I'm familiar with the core IoC concept, I'm having trouble getting the @Autowired configuration working.  I've created this github to demonstrate (stack: basic service using Jersey/Tomcat/Spring/Maven):</p>
  
  <p><a href="https://github.com/dkwestbr/autowired_example" rel="nofollow">https://github.com/dkwestbr/autowired_example</a></p>
  
  <p>I'm not using any sort of xml configuration (either for Spring or Tomcat).  I can start the server and map to my Jersey end point successfully using <code>mvn clean tomcat7:run</code> but my service is hitting a NullPointerException because the object I'm trying to Autowire isn't being initialized by the Spring framework.</p>
  
  <p>Here is the break down of my current makeup:</p>
  
  <ul>
  <li><a href="https://github.com/dkwestbr/autowired_example/blob/master/src/main/java/dkwestbr/spring/autowired/example/WebEndpoint.java" rel="nofollow">Class with object I'm trying to autowire</a></li>
  <li><a href="https://github.com/dkwestbr/autowired_example/blob/master/src/main/java/dkwestbr/spring/autowired/example/config/AppConfig.java" rel="nofollow">My configuration</a></li>
  <li><a href="https://github.com/dkwestbr/autowired_example/blob/master/src/main/java/dkwestbr/spring/autowired/example/Initializer.java" rel="nofollow">Where my config is being loaded</a></li>
  <li><a href="https://github.com/dkwestbr/autowired_example/blob/master/src/main/java/dkwestbr/spring/autowired/example/stringGetter/impl/A.java" rel="nofollow">My component/bean declaration</a></li>
  </ul>
  
  <p><strong>Why isn't spring detecting/initializing my autowired variable?</strong>  I've been reading through tutorials for a while now and am at my wits end.  It doesn't help that most tutorials are written using xml configurations.</p>
  
  <hr>
  
  <p>Below are the specific code snippets which I'm referencing above:</p>
  
  <p>The class with the variable I'm trying to initialize via the <code>@Autowired</code> annotation:</p>
  
  <pre><code>@Path("/foo")
  public class WebEndpoint {
  
      @Autowired
      private IStringGetter getTheThing;
  
      @GET
      @Path("/bar")
      @Produces(MediaType.TEXT_HTML)
      public String getStuff() {
          System.out.println(getTheThing.getItGood());
          return String.format("&lt;html&gt;&lt;body&gt;Hello - %s&lt;/body&gt;&lt;/html&gt;", getTheThing.getItGood());
      }
  }
  </code></pre>
  
  <p>My config:</p>
  
  <pre><code>@Configuration
  @ComponentScan(basePackages = {"dkwestbr.spring.autowired.example.**"})
  public class AppConfig {
  
      @Bean
      private static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
          return new PropertySourcesPlaceholderConfigurer();
      }
  
      @Configuration
      @PropertySource("classpath:configuration.properties")
      static class Production { }
  
      @Configuration
      @Profile("test")
      @PropertySource("classpath:configuration.properties")
      static class Test { }
  }
  </code></pre>
  
  <p>Where the config is being loaded:</p>
  
  <pre><code>public class Initializer implements WebApplicationInitializer {
  
      @Override
      public void onStartup(ServletContext context) throws ServletException {
          AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
          appContext.register(AppConfig.class);
          context.addListener(new ContextLoaderListener(appContext));
  
          Map&lt;String, String&gt; filterParameters = new HashMap&lt;&gt;();
  
          // set filter parameters
          filterParameters.put("com.sun.jersey.config.property.packages", "dkwestbr.spring.autowired.example");
          filterParameters.put("com.sun.jersey.config.property.JSPTemplatesBasePath", "/WEB-INF/app");
          filterParameters.put("com.sun.jersey.config.property.WebPageContentRegex", "/(images|css|jsp)/.*");
  
          // register filter
          FilterRegistration.Dynamic filterDispatcher = context.addFilter("webFilter", new ServletContainer());
          filterDispatcher.setInitParameters(filterParameters);
          filterDispatcher.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
      }
  
  }
  </code></pre>
  
  <p>The <code>@Configuration</code>/<code>Bean</code> definition that I'm hoping would map to the <code>@Autowired</code> variable:</p>
  
  <pre><code>@Component
  public class A implements IStringGetter {
  
      @Bean
      public IStringGetter getTheThing() {
          return new A();
      }
  
      @Override
      public String getItGood() {
          return "I am an A";
      }
  
  }
  </code></pre>
  
  <p><strong>UPDATE</strong>: I'm providing the stack trace on request (This is line 20: <code>System.out.println(getTheThing.getItGood());</code>)....</p>
  
  <pre><code>java.lang.NullPointerException
      dkwestbr.spring.autowired.example.WebEndpoint.getStuff(WebEndpoint.java:20)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      java.lang.reflect.Method.invoke(Method.java:606)
      com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
      com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
      com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
      com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
      com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
      com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
      com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
      com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
      com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
      com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
      com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
      com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
      com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
      com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
      com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  </code></pre>