Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a CDI bean lazily initialized?

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?

like image 836
Xorty Avatar asked Sep 27 '12 21:09

Xorty


People also ask

How can you configured to lazily initialized the bean in spring?

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.

What is lazy initialization in spring?

@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.

When lazy initialization is enabled beans are created as they are needed rather than during application startup?

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.

What is CDI managed bean?

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.


1 Answers

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?

like image 161
Jan Galinski Avatar answered Nov 11 '22 19:11

Jan Galinski