Java EE 6 @Inject lazy? [duplicate]

2019-04-09 01:24发布

问题:

This question already has an answer here:

  • How to make a CDI bean lazily initialized? 3 answers

I am doing some refactoring and reviewing of the application that we are currently developing. While doing this I found that more beans are injected and I think making they loading in an lazy manner would be better suited for their purpose. I am using Java EE 6 and tend to use more CDI than EJB injection.

So the question is whether it is possible to inject the beans lazily and if so, how can you do it?

回答1:

What about using

@Inject
private Instance<?> lazyProvider;

?

That allows Getting instance of "?" when actually needed via

lazyProvider.get();