title: Spring Context init (java)
body: <p>I'm a beginner with spring and I am having problem with basic context init and I can't find any easy tutorial.
  So what I want to do is basic dependency injection and I have seen I only have to mark classes as @Componant or @Service and inject them directly within target fiels with @Autowired.
  For this I need to set properly my spring context (I'll need to build a web service + use JPA so I want to build a webapp)</p>
  
  <p>I'm using JDK 1.8, maven 3.2.1</p>
  
  <p>I want to do a basic dependency injection.
  => I want to inject a storeImpl instance there but this lead to a null pointer exception
  => this work perfectly without injection ( private Store storeImpl = new storeImpl();  )</p>
  
  <pre><code>package com.mycompany.app.controller;
  
  @Controller
  public class Controller
  {
      @Autowired
      private Store storeImpl;
  
      public void use()
      {
          storeImpl.get();
      }
  }
  
  
  package com.mycompany.app.store;
  
  public interface Store {
      boolean get();
  
  }
  
  
  package com.mycompany.app.store;
  
  @Component
  public class StoreImpl implements Store {
      @Override
      public boolean get()
      {
          return true;
      }
  }
  </code></pre>
  
  <p><strong>=> my main (I test my app with this for now):</strong></p>
  
  <pre><code>public class App
  {
      public static void main(String[] args)
      {
          Controller controller = new Controller();
          controller.use();
      }
  }
  </code></pre>
  
  <p>=> my pom.xml</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.mycompany.app&lt;/groupId&gt;
    &lt;artifactId&gt;my-app&lt;/artifactId&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;name&gt;my-app&lt;/name&gt;
    &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
      &lt;properties&gt;
          &lt;spring.version&gt;4.0.4.RELEASE&lt;/spring.version&gt;
      &lt;/properties&gt;
  
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;junit&lt;/groupId&gt;
            &lt;artifactId&gt;junit&lt;/artifactId&gt;
            &lt;version&gt;3.8.1&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
  
        &lt;dependency&gt;
            &lt;groupId&gt;org.jsoup&lt;/groupId&gt;
            &lt;artifactId&gt;jsoup&lt;/artifactId&gt;
            &lt;version&gt;1.7.1&lt;/version&gt;
        &lt;/dependency&gt;
  
        &lt;dependency&gt;
            &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
            &lt;artifactId&gt;lombok&lt;/artifactId&gt;
            &lt;version&gt;1.12.6&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
  
  
  
  
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework&lt;/groupId&gt;
            &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
            &lt;version&gt;${spring.version}&lt;/version&gt;
        &lt;/dependency&gt;
  
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework&lt;/groupId&gt;
            &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
            &lt;version&gt;${spring.version}&lt;/version&gt;
        &lt;/dependency&gt;
  
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework&lt;/groupId&gt;
            &lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
            &lt;version&gt;${spring.version}&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework&lt;/groupId&gt;
            &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
            &lt;version&gt;${spring.version}&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework&lt;/groupId&gt;
            &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
            &lt;version&gt;${spring.version}&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework&lt;/groupId&gt;
            &lt;artifactId&gt;spring-aop&lt;/artifactId&gt;
            &lt;version&gt;${spring.version}&lt;/version&gt;
        &lt;/dependency&gt;
  
  
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
            &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
            &lt;version&gt;1.5.2.RELEASE&lt;/version&gt;
        &lt;/dependency&gt;
  
  
        &lt;dependency&gt;
            &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
            &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
            &lt;version&gt;4.3.5.Final&lt;/version&gt;
        &lt;/dependency&gt;
  
        &lt;dependency&gt;
            &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
            &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
            &lt;version&gt;3.0.1&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
  
    &lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
          &lt;version&gt;3.1&lt;/version&gt;
              &lt;configuration&gt;
                  &lt;source&gt;1.8&lt;/source&gt;
                  &lt;target&gt;1.8&lt;/target&gt;
              &lt;/configuration&gt;
        &lt;/plugin&gt;
      &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>=> my web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://java.sun.com/xml/ns/javaee"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
           http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0"&gt;
  
      &lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;
              org.springframework.web.servlet.DispatcherServlet
          &lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/META-INF/spring-context.xml&lt;/param-value&gt;
  
      &lt;/context-param&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;
              org.springframework.web.context.ContextLoaderListener
          &lt;/listener-class&gt;
      &lt;/listener&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>=> my spring-context.xml</strong></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.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc.xsd"&gt;
  
      &lt;!--&lt;context:component-scan base-package="com.mycompany.app"/&gt;--&gt;
      &lt;context:component-scan base-package="com.mycompany.app"&gt;
         &lt;!-- &lt;context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /&gt;--&gt;
      &lt;/context:component-scan&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>=> my project architecture</strong></p>
  
  <hr>
  
  <pre><code>my-app
    src
      main
        java
          com.mycompagny.app
            controller
              Controller.java
            store
              Store.java
              StoreImpl.java
  
            app.java
  
         ressources
           META-INF
             spring-context.xml
  
         webapp
           WEB-INF
             web.xml
  
  
    test
     java
  
  pom.xml
  </code></pre>
  
  <hr>
  
  <p>=> stacktrace of main()</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at com.mycompany.app.controller.Controller.run(Controller.java:25)
      at com.mycompany.app.App.main(App.java:19)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
  </code></pre>
  
  <p>So my problem is my injected field (storeImpl) is null.
  And I think I miss something with context init ?
  Thanks again for your help and yourr grats answer</p>