title: Why is an @Autowire null in a bean retrieved from ApplicationContext?
body: <p>I have studied many other StackOverflow posts on Spring autowire issues. This question is not so simple as "don't expect @Autowire to work on an object you <code>new</code>".</p>
  
  <p>I have a library that constructs objects from class names. The object classes must extend class
  F. The library is not built with Spring. It accepts a class loader for resolving the class names. It is doing this:</p>
  
  <pre><code>     Class&lt;?&gt; someClass = Class.forName(className, true, classLoader);
       if (F.class.isAssignableFrom(someClass)) {
            Class&lt;? extends F&gt; fClass = (Class&lt;? extends F&gt;) someClass; 
            Constructor constructor = fClass.getConstructor(FMap.class, Object.class);
            F fobject = (F) constructor.newInstance(parameterMap, applicationContext);
        ...
  </code></pre>
  
  <p>The library allows some context to be passed in. Here I provide the Spring ApplicationContext.
  A particular subclass of F has code like this in its constructor:</p>
  
  <pre><code>    ApplicationContext applicationContext = (ApplicationContext)context;
      X xbean = (X)applicationContext.getBean("xbean");
      S sobject = new S(xbean);
  </code></pre>
  
  <p>Class X is defined like this:</p>
  
  <pre><code>    @Component
      public class X {
  
      private final Y ybean;
  
      @Autowired
      public X(Y ybean) {
          this.ybean = ybean;
      }
      ...
  </code></pre>
  
  <p>Class Y is another @Component.</p>
  
  <p>The <code>sobject</code> is able to call a method in the <code>xbean</code> (xbean is not null), but the <code>ybean</code> is null. Why?</p>
  
  <p>I thought that Spring constructed and wired up the @Components at application start
  time. I expected that using an object from ApplicationContext would
  give me a completely built and wired up object.</p>
  
  <p><strong>Updates 06/03/2020</strong>
  I added an @PostConstruct as suggested in the first answer. That showed that the object is getting constructed early in the life of the application.</p>
  
  <p>I tried disabling <code>spring.devtools.restart.enabled</code> with no change in behavior.</p>
  
  <p>I tried disabling <code>spring.main.lazy-initialization</code> with no change in behavior.</p>
  
  <p>I removed spring-boot-devtools from my build.gradle with no change in behavior. However, the class loader being reported when the F objects and the Y bean are constructed is <code>sun.misc.Launcher$AppClassLoader</code></p>
  
  <p>We are running org.springframework.boot:spring-boot-starter-web:2.2.6.RELEASE.</p>