Issue
How can I set this attribute in my JavaConfig application context?
<beans default-lazy-init="true">
<!-- no beans will be pre-instantiated... -->
</beans>
Solution
The Spring org.springframework.context.annotation.Layz
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 {
}
Answered By - Ralph
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)