20

I have a question about the @Inject annotation in java ee 6 :

What is the difference between :

@Inject
private TestBean test;

@Inject
private Instance<TestBean> test2;

To have the reference :

test2.get();

Some infos about Instance : http://docs.oracle.com/javaee/6/api/javax/enterprise/inject/Instance.html

Maybe it's doesnt create the object until it's called by get() ? I just wanted to know which one is better for the jvm memory. I think direct @Inject will directly create an instance of the object , even if it's not used by the appplication...

Thank you !

Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84
jihedMaster
  • 331
  • 1
  • 3
  • 11
  • 1
    Was my answer sufficient or did I possibly misunderstand your question? If by chance you did find it useful, don't forget to vote it up and/or mark it as accepted. Thanks! – Jonathan S. Fisher Feb 15 '12 at 19:29

2 Answers2

12

The second is what's called deferred injection or initialization. Your container will elect do do the work of locating, initializing, and injecting the proper object for TestBean until you call get() in most circumstances.

As far as "which one is better", you should defer to the rules of optimization. Don't optimize until you have a problem, and use a profiler.

Another words, use the first one unless you can definitively prove the second one is saving you significant amounts of memory and cpu.

Let me know if that answers your question!

Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84
  • Thx for the reply, in fact I wanted to use this for the Vaadin framework ( based on GWT ) and I wanted to know if putting all UI components as Inject will kill performences like : @Inject LoginScreen login screen In fact, at the start of the app, he will load all injections on the jvm which can cause less performences if user doesnt use them... – jihedMaster Feb 16 '12 at 00:16
  • 1
    I honestly doubt you would see a performance impact from direct injection. I would go that route for sure and not risk the technical debt! I've written massive DI injected applications with over 50 beans in spring and saw little impact from DI. – Jonathan S. Fisher Feb 16 '12 at 02:07
  • So you mean that doing a lot of Inject annotation in jee 6 does not decrease the performences for the client ? – jihedMaster Feb 17 '12 at 11:56
  • Yes. Doing a lot of injections is unlikely to affect the user experience. Try it for yourself; in the unlikely for some reason manage to affect the user experience, just come back to stack overflow and we can help you use visualvm to profile your code. – Jonathan S. Fisher Feb 17 '12 at 15:32
  • 1
    Is it possible to inject a stateful instance into a method in a stateless bean? I would like to bind the instance to the method context only. – thomas.mc.work Apr 04 '16 at 13:47
  • If you want to bind an instance to a method context, there is no scope to do that in the vanilla CDI framework. BUT nothing is stopping you from implementing your own scopes :) And guess what? David Blevins of Tomitribe did a method scoped impl: https://tomitribe.io/projects/microscoped – Jonathan S. Fisher Jun 23 '20 at 14:28
11

Further information on use cases for Instance can be found in documentation:

In certain situations, injection is not the most convenient way to obtain a contextual reference. For example, it may not be used when:

  • the bean type or qualifiers vary dynamically at runtime
  • there may be no bean which satisfies the type and qualifiers
  • we would like to iterate over all beans of a certain type

This is pretty cool so you can do something like

@Inject @MyQualifier Instance<MyType> allMycandidates;

So you can obtain an Iterator from allMyCandidates and iterate over all the qualified objects.

Rafael
  • 2,521
  • 2
  • 33
  • 59