# The Java EE 7 Tutorial

# 25.8 Using Stereotypes in CDI Applications

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 stereotype is a kind of annotation that specifies the following:

- A default scope

- Zero or more interceptor bindings

- Optionally, a `@Named` annotation, guaranteeing default EL naming

- Optionally, an `@Alternative` annotation, specifying that all beans with this stereotype are alternatives

A bean annotated with a particular stereotype will always use the specified annotations, so you do not have to apply the same annotations to many beans.

For example, you might create a stereotype named `Action`, using the `javax.enterprise.inject.Stereotype` annotation:

    @RequestScoped
    @Secure
    @Transactional
    @Named
    @Stereotype
    @Target(TYPE)
    @Retention(RUNTIME)
    public @interface Action {}

All beans annotated `@Action` will have request scope, use default EL naming, and have the interceptor bindings `@Transactional` and `@Secure`.

You could also create a stereotype named `Mock`:

    @Alternative
    @Stereotype
    @Target(TYPE)
    @Retention(RUNTIME)
    public @interface Mock {}

All beans with this annotation are alternatives.

It is possible to apply multiple stereotypes to the same bean, so you can annotate a bean as follows:

    @Action
    @Mock
    public class MockLoginAction extends LoginAction { ... }

It is also possible to override the scope specified by a stereotype, simply by specifying a different scope for the bean. The following declaration gives the `MockLoginAction` bean session scope instead of request scope:

    @SessionScoped
    @Action
    @Mock
    public class MockLoginAction extends LoginAction { ... }

CDI makes available a built-in stereotype called `Model`, which is intended for use with beans that define the model layer of a model-view-controller application architecture. This stereotype specifies that a bean is both `@Named` and `@RequestScoped`:

    @Named
    @RequestScoped
    @Stereotype
    @Target({TYPE, METHOD, FIELD})
    @Retention(RUNTIME)
    public @interface Model {}