Java Reference
In-Depth Information
messageB.setMessage("This is message B");
System.out.println("Start service call " + messageA.getMessage());
System.out.println("Start service call " + messageB.getMessage());
}
}
In the EventService class shown in Listing 6-15, the containers inject the two beans produced by
the factory into the messageA and A messageB member variables of the EventService class. You can
use these objects as you would normally.
An alternative implementation is to use the @Qualifier and @interface annotations to mark the
type you want to inject. The example that follows uses custom annotations to create two qualii ers:
@LongMessage in Listing 6-16 and @ShortMessage in Listing 6-17.
LISTING 6‐16: ShortMessage qualii er
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD})
public @interface ShortMessage {}
LISTING 6‐17: LongMessage qualii er
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD})
public @interface LongMessage {}
You use these qualii ers to annotate the producer methods as shown in Listing 6-18 and their
matching injection points as shown in Listing 6-19.
LISTING 6‐18: Using the qualii ers to disambiguate the beans
public class EventProducer {
@Produces @ShortMessage
private MessageA messageAFactory(){
return new MessageA();
}
@Produces @LongMessage
private MessageB messageBFactory(){
return new MessageB();
}
}
Search WWH ::




Custom Search