Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Problem with using @Inject in custom classes.
Problem with using @Inject in custom classes. [message #976309] Thu, 08 November 2012 13:31 Go to next message
Dmitry A. is currently offline Dmitry A.
Messages: 6
Registered: November 2012
Junior Member
Hi. I have a problem when i try to inject some service in my own class. I mean that @Inject works fine when i put it in the class that registered in the application model (for example: part uri class), but when i inject IEventBroker or any another service in my own class it returns null. May be i need to register my class somewhere?
Re: Problem with using @Inject in custom classes. [message #976339 is a reply to message #976309] Thu, 08 November 2012 14:02 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph Keimel
Messages: 354
Registered: December 2010
Location: Germany
Senior Member
You can use ContextInjectionFactory to register your own instances with DI. ContextInjectionFactory.make creates an object of the given type while ContextInjectionFactory.inject will use an existing instance.
Re: Problem with using @Inject in custom classes. [message #977431 is a reply to message #976339] Fri, 09 November 2012 09:28 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

The injections will work fine in a given class as long as the Eclipse DI is in charge of instantiating it. All the classes in the app model are handled by DI. As Christoph said our own classes may be handed to the DI by instantiating them with CIF.make(class,context) where context is the IEclipseContext the resulting instance will end up going to (and the leaf context where the injection lookup will occur to satisfy your class' dependencies).
Re: Problem with using @Inject in custom classes. [message #977556 is a reply to message #977431] Fri, 09 November 2012 11:27 Go to previous messageGo to next message
Dmitry A. is currently offline Dmitry A.
Messages: 6
Registered: November 2012
Junior Member
Thanks for your advise. It helps me. But there is a little problem. When i use CIF.inject(myObject, context) - it works. But if i want to register my class using CIF.make(MyClass.class, context) i have error in console:
org.eclipse.e4.core.di.InjectionException: org.eclipse.e4.core.di.InjectionException: Could not find satisfiable constructor in MyProject.MyPackage.MyClass.
What i need to change in my construstor? Or i need to annotate it with some annotation?
Re: Problem with using @Inject in custom classes. [message #977561 is a reply to message #977556] Fri, 09 November 2012 11:29 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

with @Inject
Re: Problem with using @Inject in custom classes. [message #977628 is a reply to message #977561] Fri, 09 November 2012 12:43 Go to previous messageGo to next message
Dmitry A. is currently offline Dmitry A.
Messages: 6
Registered: November 2012
Junior Member
So if i correctly understand:
1) I can use ContextInjectionFactory.inject(MyObject, context) with concrete object if i need to use constructor with parameters;
2) I can use ContextInjectionFactory.make(MyClass, context) to create object using constructor without parameters;
3) Once i сall CIF.inject or CIF.make automatically register MyClass in Eclipse DI. And if i make object of MyClass in any other class @Inject that i use in MyClass will work? And this is the correct an good practice?
Re: Problem with using @Inject in custom classes. [message #977699 is a reply to message #977628] Fri, 09 November 2012 13:53 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5332
Registered: July 2009
Senior Member
No that's not correct, you can use DI with objects who have any number
constructor arguments. You only need to:
a) make sure you annotate your constructor with @Inject
b) make sure all values used in the constructor are in the context or
provided by a context function

Tom

Am 09.11.12 13:43, schrieb Dmitry A.:
> So if i correctly understand:
> 1) I can use ContextInjectionFactory.inject(MyObject, context) with
> concrete object if i need to use constructor with parameters;
> 2) I can use ContextInjectionFactory.make(MyClass, context) to create
> object using constructor without parameters;
> 3) Once i сall CIF.inject or CIF.make automatically register MyClass in
> Eclipse DI. And if i make object of MyClass in any other class @Inject
> that i use in MyClass will work? And this is the correct an good practice?
Re: Problem with using @Inject in custom classes. [message #981253 is a reply to message #977699] Mon, 12 November 2012 09:23 Go to previous messageGo to next message
Dmitry A. is currently offline Dmitry A.
Messages: 6
Registered: November 2012
Junior Member
Thanks a lot. But i need to understand one thing. Once i call CIF.make(MyClass, context) or CIF.inject(MyObject, context) i automatically register MyClass to the DI for the whole lyfecycle of my app?

[Updated on: Mon, 12 November 2012 10:09]

Report message to a moderator

Re: Problem with using @Inject in custom classes. [message #981317 is a reply to message #981253] Mon, 12 November 2012 10:17 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5332
Registered: July 2009
Senior Member
Yes it is registered for the Lifecycle of the IEclipseContext which is
an important difference. e.g. for an MPart-IEclipseContext it is
registered as long as the part is shown.

The only context that lifes as long as your application instance is the
one attached the MApplication.

BTW: This doesn't push it in the IEclipseContext, so if you want to use
the created instance for another injection you need to set it into the
context your own.

Tom

Am 12.11.12 10:23, schrieb Dmitry A.:
> Thanks a lot. But i need do understand one thing. Once i call
> CIF.make(MyClass, context) or CIF.inject(MyObject, context) i
> automatically register MyClass to the DI for the whole lyfecycle of my app?
Re: Problem with using @Inject in custom classes. [message #982712 is a reply to message #981317] Tue, 13 November 2012 11:03 Go to previous message
Dmitry A. is currently offline Dmitry A.
Messages: 6
Registered: November 2012
Junior Member
Thanks a lot. It helped me.
Previous Topic:theming full eclipse?
Next Topic:Codesigning Eclipse 4.x releases
Goto Forum:
  


Current Time: Tue Sep 23 18:36:01 GMT 2014

Powered by FUDForum. Page generated in 0.02454 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software