Reputation: 632
I have the following code:
public interface DummyInterface {
}
and
@Singleton
@Creatable
public class DummyInterfaceImpl1 implements DummyInterface {
}
And when I want I can simply inject this, and it works just fine, (see below):
@Inject
DummyInterfaceImpl1
However I can't do
@Inject
DummyInterface
Because I get an
Unable to process "ClassWhereIInject.dummyInterface": no actual value was found for the argument "DummyInterface".
So, I am trying to understand, if I use the combination of @Creatable and @Singleon, without adding the instance that I want to inject in the IEclipseContext, then I can only inject implementation classes and not interfaces?
I can see how this can get problematic, especially when one has multiple implementation classes for the same interface, and the dependency injection framework doesn't know which to inject...that is if you don't use the @Named annotation to specify...
Upvotes: 0
Views: 304
Reputation: 111142
The injection system only looks for something with the name you specify. It does not try and find a class that happens to implement that interface. So no you can't use an @Creatable class with a different name to the interface.
An alternative is to use a 'ContextFunction'. This is a function which is called when the injection system is looking for a name. The context function can create an instance of something suitable and put it in the context for the injector. Full details on context function are here
Upvotes: 1