User Tools

Site Tools


Additional Values for Dependency Injection

If you want to create an instance of a class by yourself but want to inject stuff from e4 then you can use stuff like

YourClass instance = ContextInjectionFactory.make(YourClass.class, context);

The context is most of the time the inject IEcliseContext.

@Inject private IEclipseContext context;

But some classes have the need to have some values passed to the constructor or injected into the fields which are not part of the IEclipseContext instance.

You can pass an additional context to the make() method.

YourClass instance = ContextInjectionFactory.make(Yourclass.class, context, additionalContext);

But where to get this additional context? Eclipse e4 provides a EclipseContextFactory class which lets you create fresh new context instances or new context instances based on existing contexts.

IEclipseContext additionalContext = EclipseContextFactory.create();

Now you can put your values into the context with the set() method.

Inject by Class

Default is to lookup the injected object by its class if your are plainly using @Inject.

Inject by Name

If you have to inject multiple objects with the same class or want to be sure that you get a specific object injected you can use the @Named annotation. You add the value to the context with context.set(name, object).

@Inject @Named("inputUri") private String inputUri;
additional_di_values.txt · Last modified: 2014/01/11 11:20 by mihael