For those coming here and are using Java config you can set the Bean to lazy-init using annotations like this:
In the configuration class:
@Configuration
// @Lazy - For all Beans to load lazily
public class AppConf {
@Bean
@Lazy
public Demo demo() {
return new Demo();
}
}
For component scanning and auto-wiring:
@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;
.......
}