Java Reference
In-Depth Information
@MyEvent(MyEvent.Type.MESSAGE)
public String messageBFactory(){
return "Another message";
}
}
You use these annotations to annotate the producer methods and their matching injection points as
shown in Listing 6-22.
LISTING 6‐22: Injecting the created beans using custom annotations to disambiguate
package com.devchronicles.observer;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.enterprise.event.Event;
import javax.inject.Inject;
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class EventService {
@Inject
@MyEvent(MyEvent.Type.LOGGING)
private String messageA;
@Inject
@MyEvent(MyEvent.Type.MESSAGE)
private String messageB;
public void startService(){
System.out.println("Start service call " + messageA);
System.out.println("Start service call " + messageB);
}
}
A simpler approach would be to use the @Named annotation rather than creating your own
annotation type. This is implemented as in Listing 6‐23.
LISTING 6‐23: Using @Named annotations to disambiguate
package com.devchronicles.factory;
import javax.enterprise.inject.Produces;
public class EventProducer {
@Produces
@Named("Logging")
public String messageAFactory(){
continues
Search WWH ::




Custom Search