Dependency Injection outside Application Model [message #1047588] |
Tue, 23 April 2013 08:20  |
Eclipse User |
|
|
|
DI works fine in Eclipse 4.2 RCP for Parts and Handers that are defined within the Application.e4xmi. eg:
public class SamplePart {
private Label label;
private TableViewer tableViewer;
@PostConstruct
public void createComposite(Composite parent) {
initialise();
.....
However when I call a class that is included within my plugin but is not defined as a part or handler such as this:
import javax.inject.Inject;
import org.eclipse.e4.core.services.log.Logger;
public class DataLogger {
@Inject Logger log;
public DataLogger(){
log.info("Message");
........
I get a org.eclipse.e4.core.di.InjectionException: java.lang.NullPointerException
Is there a way to register POJO's (I have a ton of classes that need to use Logger but have nothing to do with the UI) such that they can use DI?
|
|
|
|
|
|
|
|
Re: Dependency Injection outside Application Model [message #1048998 is a reply to message #1048985] |
Thu, 25 April 2013 04: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.
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.31714 seconds