I am using Weld implementation of CDI 1.0 and I cannot find way how to make bean lazy like in Spring (using @Lazy
or lazy-init
in XML). Is there a way how to tell CDI's Injector
not to initialize bean on startup?
Setting the property value to true means that all the beans in the application will use lazy initialization. This configuration affects all the beans in the context. So, if we want to configure lazy initialization for a specific bean, we can do it through the @Lazy approach.
@Lazy annotation indicates whether a bean is to be lazily initialized. It can be used on @Component and @Bean definitions. A @Lazy bean is not initialized until referenced by another bean or explicitly retrieved from BeanFactory . Beans that are not annotated with @Lazy are initialized eagerly.
Lazy initialization can result in significantly reduced startup times as fewer classes are loaded and fewer beans are created during application startup. For example, a small web application that's using Actuator and Spring Security that normally starts in 2500ms will start in 2000ms with lazy initialization enabled.
A CDI bean is a POJO, plain old java object, that has been automatically instantiated by the CDI container, and is injected into all, and any qualifying injection points in the application. The CDI container initiates the bean discovery process during deployment.
See my answer on: http://www.adam-bien.com/roller/abien/entry/lazy_injection_with_javax_inject
Using
@Inject
Instance<MyObject> object;
the bean is initialized only when needed ... isn't that what you want?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With