How to make a CDI bean lazily initialized?

I am using the Weld CDI 1.0 implementation, and I cannot find a way to make the bean lazy, like in Spring (using @Lazyor lazy-initin XML). Is there a way to tell CDI Injectornot to initialize a bean at startup?

+5
source share
3 answers

No, this is not possible in CDI. The closest thing you could get is to create a new version of InjectionPoint (using the extension) that gives a proxy server, and the proxy server will initialize everything the first time the method is called.

+7
source

See my answer: http://www.adam-bien.com/roller/abien/entry/lazy_injection_with_javax_inject

Using

 @Inject
Instance<MyObject> object;

bean , ... , ?

+5

If the bean you inject into the normal area (@SessionScoped, @RequestScoped, etc.), it will be lazily instantiated. What you get in your bean client is a proxy server that does not point to a specific instance until when you call the method on the proxy server.

As others have already pointed out, @Inject Instance<MyBean> myBeanInstance;it can also be used to create an explicit lazy copy.

+1
source

All Articles