Maciej
Maciej

Reputation: 595

@Created annotation in Java

Going through JBossTicket Monster tutorial I've stumbled upon @Created annotation and I couldn't find any information about it. As far as I know it's not implemented anywhere in the project.

@Path("/bookings")
@Stateless
public class BookingService extends BaseEntityService<Booking> {

    @Inject
    SeatAllocationService seatAllocationService;

    @Inject @Created
    private Event<Booking> newBookingEvent;

    public BookingService() {
        super(Booking.class);
    }
}

Could someone please explain what does it do?

Upvotes: 0

Views: 387

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

There is no @Created in Java nor in CDI. This is a custom qualifier that you create to indicate which implementation to use when injecting the element.

If you read further in the example, there's an explanation about what @Created means:

We would like other parts of the application to be aware of the fact that a new booking has been created, therefore we use the CDI to fire an event. We do so by injecting an Event instance into the service (indicating that its payload will be a booking). In order to individually identify this event as referring to event creation, we use a CDI qualifier, which we need to add:

src/main/java/org/jboss/jdf/example/ticketmonster/util/qualifier/Created.java

/**
* {@link Qualifier} to mark a Booking as new (created).
*/
@Qualifier
@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Created {
}

What are qualifiers?

CDI uses a type-based resolution mechanism for injection and observers. In order to distinguish between implementations of an interface, you can use qualifiers, a type of annotations, to disambiguate. Injection points and event observers can use qualifiers to narrow down the set of candidates

Upvotes: 1

Related Questions