Spring bean is created, but is null when Autowired
<p>I am trying to inject an object to a class. However, the field is always null. I tried <code>@Autowired</code> and <code>@Resource</code> annotations. I am not creating the object with <code>new</code> operator anywhere. The constructor of <code>Foo</code> is called properly.</p>

<p>The minimal example of this problem:</p>

<p><strong>Foo class</strong></p>

<pre><code>package foo.bar;
public class Foo {
    Foo(){
        System.out.println("Foo constructor");
    }
    public void func() {
        System.out.println("func()");
    }
}
</code></pre>

<p><strong>Bar class</strong></p>

<pre><code>package foo.bar;
public class Bar {
    @Autowired
    private Foo foo;

    public Bar() {
        foo.func();
    }
}
</code></pre>

<p><strong>Entry point</strong></p>

<pre><code>package foo.bar;
public class HelloApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    }
}
</code></pre>

<p><strong>spring-config.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"&gt;
    &lt;context:component-scan base-package="foo.bar"/&gt;
    &lt;bean id = "foo" class="foo.bar.Foo" /&gt;
    &lt;bean id = "bar" class="foo.bar.Bar" /&gt;
&lt;/beans&gt;
</code></pre>

<p>Why is <code>foo</code> field of <code>Bar</code> class always <code>null</code>? How can I fix this?</p>