|
|
|
Re: Dependency Injection outside Application Model [message #1048399 is a reply to message #1047955] |
Wed, 24 April 2013 12:11 |
Eclipse User |
|
|
|
It doesn't work that way. The context you are creating out of the blue contains nothing. Contexts are the suppliers of the injections and, being empty, they spawn the InjectionException in your case. The objects that get injected in 'Eclipse-powered' classes are splattered in a big context hierarchy (there are a lot of posts around here in the forums talking about the context hierarchy, just search for them) and what you get (or whether you get it) depends on where you are (more specifically in which context you ask for injection).
The context you want to provide your domain classes to when you call CIF#make or CIF#inject has to be linked to this big hierarchical context tree (through the createChildContext method). That way you will have access to the 'default' objects stored throughout this tree.
|
|
|
|
|
Re: Dependency Injection outside Application Model [message #1048998 is a reply to message #1048985] |
Thu, 25 April 2013 08:08 |
Eclipse User |
|
|
|
The context hierarchy is a simple tree-like data structure. Injection searches for objects in contexts in a leaf-to-root fashion. Suppose you have root R and children C1,C2,C3. Child C1 has children D2,D3,D4. D3 has childs E1,E2,E3. If you require something to be injected in your class in the E3 context the DI engine will do a lookup on E3, if it doesn't find it will search on D3, then C1, then R, then OSGi Bundle Context, then -> InjectionException.
So, if you want to have access to these nodes of context grab one of these contexts, say E3 and do E3#createChild. Say the resulting context is P. Use this P to do the CIF#make and CIF#inject and now the lookup sequence will be P-E3-D3-C1-R-OSGi and as a result you will have access to all the objects that are in those contexts.
These R-C-D-E are just notations for ease of understanding and in reality they are MApplication's (R), MWindow's (C), MPerspective's (D) and MPart's (E) context. Handlers are executed in the active part's context.
|
|
|
|
|
Re: Dependency Injection outside Application Model [message #1052417 is a reply to message #1051775] |
Tue, 30 April 2013 03:47 |
Don Smyth Messages: 35 Registered: April 2013 |
Member |
|
|
Thanks Sopot,
I followed up your hierachy and I understand the E4/OSGi relationship a little more.
The 2 ways I could access the E4 model are then
A) using the EclipseContextFactory
1. Bundle bundle = FrameworkUtil.getBundle(POJO.class);
2. BundleContext bundleCntxt = bundle.getBundleContext();
3. IEclipseContext serviceCntxt = EclipseContextFactory.getServiceContext(bundleCntxt);
However line 2. throws a null error. I think I need to register the plugin that needs E4 service access as an OSGi consumer. I am working on this at the moment but haven't quite worked how to implement it yet.
B) If I follow correctly as per your hierachy tree, call eg. E3 in the hierachy from a POJO and do E3#createChild. Use the resultant context P do "ContextInjectionFactory.inject(POJO, context);"
B sounds like a better option to me. I hadn't quite understood it the first time I read it. I will follow up tonight. Thanks again.
[Updated on: Tue, 30 April 2013 03:51] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.07085 seconds