toiepp
toiepp

Reputation: 85

Is there any annotation equivalent of "default-lazy-init" attribute in Spring Framework?

How can I set this attribute in my JavaConfig application context?

<beans default-lazy-init="true">
    <!-- no beans will be pre-instantiated... -->
</beans>

Upvotes: 1

Views: 261

Answers (1)

Ralph
Ralph

Reputation: 120811

The Spring org.springframework.context.annotation.Lazy annotation indicates whether a bean is to be lazily initialized.

You can add it to a @Configuration class, a @Bean method or a @Component (for example @Service annotated class)

Example for a single bean:

@Configuration
public class MyConfig {

   @Bean
   @Lazy
   public Example myLayzBean() {
        return new Example();
   }
  
}

Example for all beans in one configuration class

@Configuration
@Lazy
public class MyConfig {

   @Bean
   public Example1 myLayzBean1() {
        return new Example1();
   }

   @Bean
   public Example2 myLayzBean2() {
        return new Example2();
   }
  
}

Example for bean found by component scan

@Service
@Lazy
public class Example3 {
  
}

Upvotes: 1

Related Questions