title: How to Lazy load all the Spring beans whether it is defined by @Bean or @Component in Springboot 2.2
body: <p>I am writing a spring application which is interactive and basically handles lots of commands like create, list, update, delete various types of resources. </p>
  
  <p>For now, just assume a single run of the application handles only a single command and the program exits.</p>
  
  <p>For all the classes to validate command, execute the command, required factory classes for each resource there is a separate class and each class is annotated with <code>@Component</code> annotation for spring to manage all the components. </p>
  
  <p>There are also some of the manually defined beans by <code>@Bean</code> method.</p>
  
  <p>Now that my application first identifies what kind of command is executed (create, delete, list, update, etc), I want the only beans of that command to be created and Autowired wherever required (after taking command from user) and I want to avoid the creation of dozens of beans related to other commands.</p>
  
  <p>On searching, I came to know about <em>Lazy instantiation</em> of Beans that spring provides. 
  However, I am not sure if it is the weapon I am searching for.</p>
  
  <p><strong>What I tried</strong></p>
  
  <ul>
  <li>Very first I found @Lazy annotation, but since I want to lazily load all the Beans, I don't want to write @Lazy everywhere in each class. </li>
  <li>Then I found setting below property in application.yml does the work.</li>
  </ul>
  
  <pre><code>spring:
    main:
      lazy-initialization: true
  </code></pre>
  
  <p>I tried that but still, it is not lazily creating the beans.</p>
  
  <p>My <em>application.yml</em> files looks like this </p>
  
  <pre><code>spring:
    main:
      lazy-initialization: true
  </code></pre>
  
  <p>My main <em>SpringBootApplication</em> file looks like this:</p>
  
  <pre><code>@Slf4j
  @SpringBootApplication
  public class SpringBootApplication {
      public static void main(String[] args) {
          System.out.println("Loading Application...");
          ApplicationContext context = SpringApplication.run(SpringBootApplication.class, args);
  
  
  
          final AtomicInteger counter = new AtomicInteger(0);
          log.info("**************** START: Total Bean Objects: {} ******************", context.getBeanDefinitionCount());
  
          Arrays.asList(context.getBeanDefinitionNames())
                  .forEach(beanName -&gt; {
                      log.info("{}) Bean Name: {} ", counter.incrementAndGet(), beanName);
                  });
  
          log.info("**************** END: Total Bean: {} ******************", context.getBeanDefinitionCount());
      }
  }
  
  </code></pre>
  
  <p>My other classes looks like this:</p>
  
  <pre><code>
  @Component
  @RequiredArgsConstructor(onConstructor = @__(@Autowired))
  public class MyClass1 implements ResourceCreator&lt;MyClass2, MyClass3&gt; {
      private final RequestValidatorImpl requestValidator;
      private final ResourceCreator resourceCreator;
  
  @Override
  public MyClass2 toImplementFunction(MyClass3 myclass3) {
  //logic
  }
  
  </code></pre>
  
  <p>On running the application, It prints all the classes where I annotated @Component as well as beans created by @Bean method.</p>
  
  <p>I have also tried using below in <code>Application.properties</code> but still no use.</p>
  
  <pre><code>spring.main.lazy-initialization=true
  </code></pre>
  
  <p>Also, if you wish, please comment on whether I should use @Component for each Class like I am using or not and what is better practice instead.</p>