title: My spring bean is not injected or created when I use the autowire and component annotations
body: <p>I'm trying to understand DI, but my spring bean is not injected or created when I use the autowire and component annotations. Instead I get a nullpointer exception because my bean is null. Should I created it manually and in that case where? Where do you specify the order in which the beans are created, when several beans depend on each other? Here is my code:</p>
  
  <p>App.java</p>
  
  <pre><code>package se.springtest;
  
  import se.springtest.Person;
  
  import org.springframework.core.io.ClassPathResource;
  import org.springframework.beans.factory.BeanFactory;
  import org.springframework.beans.factory.xml.XmlBeanFactory;
  
  public class App 
  {
    public static void main( String[] args )
    {
      BeanFactory factory = new XmlBeanFactory(
              new ClassPathResource("application-context.xml"));
  
          Person p = (Person) factory.getBean("person");
          p.show();
  
    }
  }
  </code></pre>
  
  <p>application-context.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:jdbc="http://www.springframework.org/schema/jdbc"
     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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
  &lt;context:component-scan base-package="se.springtest"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Person.java</p>
  
  <pre><code>package se.springtest;
  
  public interface Person {
  public void show();
  }
  </code></pre>
  
  <p>PersonImpl.java</p>
  
  <pre><code>package se.springtest;
  import org.springframework.stereotype.Component;
  import org.springframework.beans.factory.annotation.Autowired;
  
  @Component("person")
  public class PersonImpl implements Person {
  private String firstName;
  private String lastName;
  private AdressInfo adress;
  
  public PersonImpl() {firstName="Olle"; lastName="Olsson";       System.out.println("creating PersonImpl object");}
  
  
  @Autowired
  public void setAdress(AdressInfo adress) {
      this.adress = adress;
  }
  
  public AdressInfo getAdress() {
      return adress;
  }
  
  public String getFirstName() {
      return firstName;
  }
  
  public void setFirstName(String firstName) {
      this.firstName = firstName;
  }
  
  public String getLastName() {
      return lastName;
  }
  
  public void setLastName(String lastName) {
      this.lastName = lastName;
  }
  
  public void show() {
      System.out.println("Name is " + getFirstName() + " " +getLastName());
      if (adress!=null)
          adress.show();
      else System.out.println("null");
  }
  }
  </code></pre>
  
  <p>AdressInfo.java</p>
  
  <pre><code>package se.springtest;
  
  public interface AdressInfo {
  public void show();
  }
  </code></pre>
  
  <p>AdressInfoImpl.java</p>
  
  <pre><code>package se.springtest;
  
  import org.springframework.stereotype.Service;
  
  @Service("adress")
  public class AdressInfoImpl implements AdressInfo {
  private String adress;
  
  public AdressInfoImpl() {adress="Storgatan 1"; System.out.println("creating AdressImpl object");}
  
  public String getAdress() {
      return adress;
  }
  
  public void setAdress(String adr) {
      this.adress = adr;
  }
  
  public void show() {
      System.out.println("Address is " + adress);  
  }
  }
  </code></pre>
  
  <p>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;se.springtest&lt;/groupId&gt;
  &lt;artifactId&gt;spring-hello&lt;/artifactId&gt;
  &lt;packaging&gt;jar&lt;/packaging&gt;
  &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  &lt;name&gt;spring-hello&lt;/name&gt;
  &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
  &lt;build&gt;
   &lt;plugins&gt;
    &lt;plugin&gt;
      &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
      &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
      &lt;executions&gt;
        &lt;execution&gt;
          &lt;goals&gt;
            &lt;goal&gt;java&lt;/goal&gt;
          &lt;/goals&gt;
        &lt;/execution&gt;
      &lt;/executions&gt;
  
      &lt;configuration&gt;
        &lt;mainClass&gt;se.springtest.App&lt;/mainClass&gt;
  
      &lt;/configuration&gt;
  
  
    &lt;/plugin&gt;
    &lt;plugin&gt;
      &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
      &lt;configuration&gt;
        &lt;source&gt;1.5&lt;/source&gt;
        &lt;target&gt;1.5&lt;/target&gt;
      &lt;/configuration&gt;
    &lt;/plugin&gt;
  
  &lt;/plugins&gt;
  &lt;/build&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.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring&lt;/artifactId&gt;
    &lt;version&gt;2.5.6.SEC02&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>And I compile it with</p>
  
  <pre><code>mvn clean compile exec:java
  </code></pre>
  
  <p>but I get </p>
  
  <pre><code>Name is Olle Olsson
  null
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>Name is Olle Olsson
  Adress is Storgatan 1
  </code></pre>
  
  <p>It would be really helpful if someone could tell me what is the problem. It would make me and maybe others understanding DI better... </p>