[ACCEPTED]-Inject list of objects in CDI (Weld)-jboss-weld

Accepted answer
Score: 70

Combining my attempts with an answer from 8 the Weld forum:

@Inject @Any
private Instance<SocialNetworkService> services;

Instance implements Iterable, so it is then 7 possible to simply use the for-each loop. The 6 @Any qualifier is needed.

Another way to do this 5 is by using the event system:

  • create a MessageEvent (containing all the information about the message)
  • instead of 4 injecting a list of social networks, simply 3 inject the event:

    @Inject private Event<MessageEvent> msgEvent;

    and fire it: msgEvent.fire(new MessageEvent(message));

  • observe the 2 event in all services (regardless of their 1 interface, which might be a plus):

    public void consumeMessageEvent(@Observes MessageEvent msgEvent) {..}
Score: 0

I had a look at the JSR-299 specification 10 and it doesn't seem that you can do what 9 you want to do easily and I do not have 8 experience enough with Weld to provide code 7 for this special case.

However, based on 6 chapter 12.3 "Bean Discovery" you might 5 be able to declare the implementations as 4 @Alternative's (to avoid Weld complain about 3 multiple implementations) and listen to 2 ProcessBean events to collect when implementations 1 of SocialNetworkService are seen.

More Related questions