facundofarias
facundofarias

Reputation: 3043

Is there a way to inject an instance into a Collection using HK2?

Let's say we have the following code:

@Inject
private Collection<SomeKind> myCollection;

What I want is that the dependencies after they were solved (I mean, all the classes that are of SomeKind type), could also being added to the collection. I know I can inject the ServiceLocator, and programmatically search for the instances on the registry, and then add them to the collection by myself, I just was wondering if there is a common mechanism for this scenario.

Upvotes: 2

Views: 331

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209072

I think what you want is an IterableProvider<SomeKind>. See the Documentation.

It's an Iterable, so you can use it in a for each loop

for (SomeKind someKind: someKinds) {
    ...
}

You could look them up by name

@Inject
IterableProvider<SomeKind> someKinds;
...
SomeKind someKind = someKinds.named("someName").get();

Here's a complete example using Jersey

Upvotes: 2

Related Questions