title: Spring boot how to use @PostConstruct correctly
body: <p>Spring boot 2.5.4 I used @PostConstruct for the very first time in my service class. As following:-</p>
  <pre><code>   @Slf4j
     @Service
     @AllArgsConstructor
  
      public class FileMonitorService {
  
      private final AppProperties appProperties;
  
      private final WatchService watchService;
  
      private final RestTemplate restTemplate;
  
     @PostConstruct
     @Async
      public void startMonitoring() {
          FileUtils.setAppProperties(appProperties);
          FileUtils.setRestTemplate(restTemplate);
          FileUtils.readFilesForDirectory();
          log.info(&quot;START_MONITORING&quot;);
          try {
              WatchKey key;
              while ((key = watchService.take()) != null) {
                  for (WatchEvent&lt;?&gt; event : key.pollEvents()) {
                      log.info(&quot;Event kind: {}; File affected: {}&quot;, event.kind(), event.context());
                     if((event.kind() == StandardWatchEventKinds.ENTRY_CREATE ||
                        event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) &amp;&amp;
                        event.context().toString().contains(&quot;.xml&quot;)){
                         try {
                             restTemplateRequest(event.context().toString()+&quot; processing&quot;);
                             FileUtils.readXml(Path.of(FileUtils.getFileAbsolutePath(appProperties.getDataIn()),
                                     event.context().toString()));
                         }catch (Exception e){
                             log.error(&quot;startMonitoring Exception: &quot;+e.getMessage());
                         }
                     }
                  }
                  key.reset();
              }
          } catch (InterruptedException e) {
              log.warn(&quot;startMonitoring: interrupted exception for monitoring service: &quot;+e.getMessage());
          }
      }
   }
  </code></pre>
  <p>This method is called as soon as app launched. That is my requirements to process all file as soon as the app starts. I have controller as following:-</p>
  <pre><code>@RestController
  @RequestMapping(&quot;/xml&quot;)
  public class FileController {
  
      @Autowired
      FileMonitorService fileMonitorService;
  
      @SneakyThrows
      @GetMapping(&quot;/restart&quot;)
      public String restartFileMonitoring(){
          fileMonitorService.startMonitoring();
          return &quot;File monitoring restarted started successfully&quot;;
      }
  }
  </code></pre>
  <p>My app starts on port 8080 and no exception at all. But when I get call this end point  <code>localhost:8080/xml/restart</code></p>
  <p>It is not reachable. If I comment out the <code>@PostConstruct</code> then I can call the end point. I am confused how to use this annotation properly. What is wrong in my code?</p>
  <p><strong>Update info:-</strong></p>
  <pre><code>    :: Spring Boot ::                (v2.5.4)
  
  2021-09-14 18:23:21.521  INFO 71192 --- [           main] c.f.i.task.BatchProcessorApplication     : Starting BatchProcessorApplication using Java 14.0.2 on dev with PID 71192 (/home/dev/Desktop/batch-processor/batch-processor/target/classes started by dev in /home/dev/Desktop/batch-processor/batch-processor)
  2021-09-14 18:23:21.523  INFO 71192 --- [           main] c.f.i.task.BatchProcessorApplication     : No active profile set, falling back to default profiles: default
  2021-09-14 18:23:22.485  INFO 71192 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
  2021-09-14 18:23:22.495  INFO 71192 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
  2021-09-14 18:23:22.495  INFO 71192 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.52]
  2021-09-14 18:23:22.564  INFO 71192 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
  2021-09-14 18:23:22.564  INFO 71192 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 988 ms
  File to monitor: /home/dev/Desktop/batch-processor/batch-processor/data/in
  2021-09-14 18:23:22.647  INFO 71192 --- [           main] c.f.i.task.config.FileMonitorConfig      : MONITORING_DIR: /home/dev/Desktop/batch-processor/batch-processor/data/in/
  2021-09-14 18:23:22.667  INFO 71192 --- [           main] c.f.i.task.service.FileMonitorService    : START_MONITORING
  </code></pre>
  <p>That is the log when I run the app. After debugging I found that <code>while ((key = watchService.take()) != null) {</code> call never returns until I copy some XML file as this app process xml files. Then I copy any xml file in the monitoring dir. I was expecting that <code>@Async</code> it will run in back ground thread in async mode. How to monitory this dir in background thread? So the caller of this method won't be blocked.</p>