How does spring dependency injection work? : <p>I want to know  when are the dependencies injected in a spring bean.
I have the following code :</p>

<pre><code>import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class TestClass {

    @Autowired
    Environment env;

    // throws Null Pointer Here.
    String prop = env.getProperty("some.property");

    public void test() {
        // works here
        String prop = env.getProperty("some.property");
    }
}
</code></pre>

<p>Why does spring throws NPE when I try to get Environment variable.
What happens when the class constructor is called? Does spring only looks for bean availability in the context at construction time and not inject it then and there? Please explain the full flow of bean creation. Are the dependencies only injected after the object is constructed and added to the context? Does spring look for dependencies only after an instance of the class is created or does it look for them when the constructor is called?</p>
