Java Reference
In-Depth Information
LISTING 6‐19: Injecting the created beans using qualii ers to disambiguate
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class ClientMessage {
@Inject @ShortMessage
private MessageA messageA;
@Inject @LongMessage
private MessageB messageB;
public void doEvent(){
messageA.setMessage("This is a long email message.");
messageB.setMessage("This is a short SMS message.");
System.out.println(messageA.getMessage());
System.out.println(messageB.getMessage());
}
}
The @Target annotation specii ed on the qualii er interface determines where you can use the
qualii er. The values can be one or all of the following— TYPE , METHOD , FIELD , and PARAMETER —and
their meanings are self‐explanatory.
Alternatively, you can achieve the same implementation via the use of an enum type dei ned in the
@interface class, Listing 6-20 shows this implementation.
LISTING 6‐20: Custom annotation type
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface MyEvent {
Type value();
enum Type{ LOGGING , MESSAGE }
}
With the help of this custom annotation, you can use different methods to create string objects
marked with your annotation. In Listing 6-21 strings are produced by the messageAFactory and
the messageBFactory methods.
LISTING 6‐21: Using the custom annotations to disambiguate the beans
public class EventProducer {
@Produces
@MyEvent(MyEvent.Type.LOGGING)
public String messageAFactory(){
return "A message";
}
@Produces
Search WWH ::




Custom Search