Spring provides an attribute called lazy-init to inform the Spring IOC container for not creating that bean at the start up. lazy-init will be set as true to indicate the container. The beans will be created only when requested.
id="..." class="..."
id="..." class="..." lazy-init="true"
id="..." class="..." init-method="..."
id="..." class="..." destroy-method="..."
The init-method attribute specifies a method that is to be called on the bean immediately upon instantiation. Similarly, destroy-method specifies a method that is called just before a bean is removed from the container.
@Configuration
// @Lazy - For all Beans to load lazily
public class AppConf {
@Bean
@Lazy
public Demo demo() {
return new Demo();
}
}
@Component
@Lazy
public class Demo {
....
....
}
@Component
public class B {
@Autowired
@Lazy // If this is not here, Demo will still get eagerly instantiated to satisfy this request.
private Demo demo;
.......
}
No comments:
Post a Comment