title: spring boot - @PostConstruct not called on @Component
body: <p>I am new to spring, and I have created a new spring boot project using <a href="https://start.spring.io/" rel="nofollow noreferrer">https://start.spring.io/</a> with no further dependencies, unzipped the zip file and opened the project in IntelliJ IDEA. I have not done any further configurations. I am now trying to setup a bean with a @PostConstruct method - however, the method is never invoked by spring.</p>
  
  <p>These are my classes:</p>
  
  <p><strong>SpringTestApplication.java</strong> </p>
  
  <pre><code>package com.habichty.test.testspring;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.ConfigurableApplicationContext;
  
  @SpringBootApplication
      public class SpringTestApplication {
  
          public static void main(String[] args) {
              ConfigurableApplicationContext context = SpringApplication.run(SpringTestApplication.class, args);
              context.getBean(TestBean.class).testMethod();
          }
      }
  </code></pre>
  
  <p><strong>TestBean.java</strong></p>
  
  <pre><code>package com.habichty.test.testspring;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.stereotype.Component;
  import javax.annotation.PostConstruct;
  
  @Component  
      public class TestBean {
              private final Logger log = LoggerFactory.getLogger(this.getClass());
              private int a = 1;
  
              public TestBean()
              {
                  log.debug("Constructor of TestBean called.");
              }
  
              @PostConstruct
              public void init()
              {
                  log.debug("init()-Method of TestBean called.");
                  a = 2;
              }
  
              public void testMethod()
              {
                  log.debug("Test Method of TestBean called. a=" + a);
              }
  
          }
  </code></pre>
  
  <p>When I start the application, this is my output:</p>
  
  <pre><code> :: Spring Boot ::        (v1.5.9.RELEASE)
  
  2018-01-22 13:15:57.960  INFO 12035 --- [           main] c.h.t.testspring.SpringTestApplication   : Starting SpringTestApplication on pbtp with PID 12035 (/home/pat/prj/testspring/testspring/target/classes started by pat in /home/pat/prj/testspring/testspring)
  2018-01-22 13:15:57.962 DEBUG 12035 --- [           main] c.h.t.testspring.SpringTestApplication   : Running with Spring Boot v1.5.9.RELEASE, Spring v4.3.13.RELEASE
  2018-01-22 13:15:57.962  INFO 12035 --- [           main] c.h.t.testspring.SpringTestApplication   : No active profile set, falling back to default profiles: default
  2018-01-22 13:15:58.018  INFO 12035 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2931522b: startup date [Mon Jan 22 13:15:58 CET 2018]; root of context hierarchy
  2018-01-22 13:15:58.510 DEBUG 12035 --- [           main] com.habichty.test.testspring.TestBean    : Constructor of TestBean called.
  2018-01-22 13:15:58.793  INFO 12035 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
  2018-01-22 13:15:58.822  INFO 12035 --- [           main] c.h.t.testspring.SpringTestApplication   : Started SpringTestApplication in 1.073 seconds (JVM running for 2.025)
  2018-01-22 13:15:58.822 DEBUG 12035 --- [           main] com.habichty.test.testspring.TestBean    : Test Method of TestBean called. a=1
  2018-01-22 13:15:58.826  INFO 12035 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2931522b: startup date [Mon Jan 22 13:15:58 CET 2018]; root of context hierarchy
  2018-01-22 13:15:58.828  INFO 12035 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
  </code></pre>
  
  <p>As you can see, spring initializes the TestBean and also executes the testMethod() - but the init()-Method, annotated with @PostConstruct, is not invoked.</p>
  
  <p>What am I doing wrong? 
  Any help is very appreciated.</p>
  
  <p><strong>UPDATE 1</strong>
  In my application.properties, I have configured:</p>
  
  <pre><code>logging.level.com = DEBUG
  </code></pre>
  
  <p>Changing this to <em>logging.level.root = DEBUG</em> results in a massively bigger log. However, it still does not contain the debug message of my init() method.</p>
  
  <p><strong>UPDATE 2</strong> Added package and import statements.</p>
  
  <p><strong>UPDATE 3</strong> To further clarify that this is not a logging issue, I have added an new int to the code that should be altered by the init()-Method. As far as I understood the concept of the @PostConstruct annotation, it should be executed prior to any other method execution. As a consequence, the output of testMethod() should now contain <em>a=2</em>. In the updated output, you may see that this is not the case.</p>
  
  <p><strong>UPDATE 4</strong> This is my POM</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &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/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;com.habichty.test.testspring&lt;/groupId&gt;
      &lt;artifactId&gt;springTest&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;name&gt;springTest&lt;/name&gt;
      &lt;description&gt;springTest&lt;/description&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.5.9.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>The output of <code>java -version</code>:</p>
  
  <pre><code>java version "9.0.1"
  Java(TM) SE Runtime Environment (build 9.0.1+11)
  Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)
  </code></pre>