22
loading...
This website collects cookies to deliver better user experience
@Retention(RUNTIME)
. Typically a qualifier type is defined as @Target({METHOD, FIELD, PARAMETER, TYPE})
.@javax.inject.Qualifier
meta-annotation.@Jabbawockee
and @LesTwins
qualifiers.@Qualifier
@Documented
@Retention(RUNTIME)
@Target({ TYPE, FIELD, METHOD, PARAMETER })
public @interface Jabbawockee {
}
@Qualifier
@Documented
@Retention(RUNTIME)
@Target({ TYPE, FIELD, METHOD, PARAMETER })
public @interface LesTwins {
}
@Jabbawockee
public class JabbawockeeService implements Service {
public void doWork() {
System.out.println("Jabbawockee doing the work.");
}
}
A stereotype is a kind of annotation, applied to a bean, that incorporates other annotations. Stereotypes can be particularly useful in large applications in which
you have a number of beans that perform similar functions.
A well-known stereotypes, known by Enterprise Java Developers, are from Spring Framework: @Component
, @Service
and @Repository
stereotypes.
@Stereotype
(@javax.enterprise.inject.Stereotype
), that packages several other annotations. For instance, to recreate the 3 stereotypes from Spring Framework into your JavaEE application:@Stereotype
@Target(TYPE)
@Retention(RUNTIME)
@Documented
@Named
public @interface Component {}
Component
stereotype has been declared with an empty @Named
annotation, which specifies that every bean with the stereotype has a defaulted name when a name is not explicitly specified by the bean. A @Named
qualifier declared by a stereotype is not added to the qualifiers of a bean with the stereotype.@Named
annotation, the container automatically detects the problem and treats it as a definition error.@Stereotype
@Target(TYPE)
@Retention(RUNTIME)
@Component //Note that Spring specifies that this annotation is a specialization of Component annotation.
public @interface Service {}
@Stereotype
@Target(TYPE)
@Retention(RUNTIME)
public @interface Repository {}
Service
stereotype with @Transactional
.@Stereotype
@Target(TYPE)
@Retention(RUNTIME)
@Transactional
@Component
public @interface Service {}
Controller
stereotype, we may specify to have a default @RequestScoped
scope type:@RequestScoped
@Stereotype
@Target(TYPE)
@Retention(RUNTIME)
@Documented
@Named
public @interface Component {}
@javax.enterprise.inject.Model
which is intended for use with beans that define the model layer of an MVC web application architecture such as Jakarta Server Faces.@Named
@RequestScoped
@Stereotype
@Target({TYPE, METHOD, FIELD})
@Retention(RUNTIME)
public @interface Model {}
@Produces
and Decorators, using the @Decorator
annotation.