How to make a CDI bean lazily initialized?
Asked Answered
H

3

6

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?

Historiated answered 27/9, 2012 at 21:47 Comment(1)
Duplicate of https://mcmap.net/q/1631521/-java-ee-6-inject-lazy-duplicateCistaceous
H
7

No, this isn't possible in CDI. The closest thing you could get would be to create a new InjectionPoint (using an Extension) implementation that gives a proxy and the proxy would initialize everything on the first method invocation.

Hinrichs answered 27/9, 2012 at 22:35 Comment(5)
So, by using @Inject Instance<beantype> beantypeProvider; this should work right ?Whittle
Um, my first reaction is to say that might work. Though if you're using a CDI 1.0 implementation Instance does leak memory of dependant scoped instances.Hinrichs
Which implementation are you talking about ? where is your reference ?Whittle
Both OWB and Weld had the problem. See #8385690 for more information. It was a both a spec and an implementation issue.Hinrichs
So, as long as I can use the Instance.destroy(instance) I am ok :) ThanksWhittle
K
8

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?

Kataway answered 11/4, 2013 at 11:36 Comment(2)
According to Adam Bien, there's an alternative to this that might be a little more lightweight: merely injecting a Provider<?> instead of an Instance<?>, all the rest being the same.Reimer
@Hein Blöd The link to the entry in Adam Bien's weblog has changed to Lazy Injection with javax.inject.Provider.Forgetful
H
7

No, this isn't possible in CDI. The closest thing you could get would be to create a new InjectionPoint (using an Extension) implementation that gives a proxy and the proxy would initialize everything on the first method invocation.

Hinrichs answered 27/9, 2012 at 22:35 Comment(5)
So, by using @Inject Instance<beantype> beantypeProvider; this should work right ?Whittle
Um, my first reaction is to say that might work. Though if you're using a CDI 1.0 implementation Instance does leak memory of dependant scoped instances.Hinrichs
Which implementation are you talking about ? where is your reference ?Whittle
Both OWB and Weld had the problem. See #8385690 for more information. It was a both a spec and an implementation issue.Hinrichs
So, as long as I can use the Instance.destroy(instance) I am ok :) ThanksWhittle
M
5

If the bean you're injecting is in a normal scope (@SessionScoped, @RequestScoped etc), it will be lazily instantiated. What you get in your client bean is a proxy that doesn't point to a concrete instance until the first time you invoke a method on the proxy.

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

Milliard answered 9/11, 2015 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.