Adding annotations to a web application

Java EE 5 and 6 support the injection of annotations into your source code, so that you can embed resources, dependencies, services, and lifecycle notifications in your source code, without having to maintain these artifacts elsewhere. Annotations simplify the development and configuration of enterprise applications.

About this task

You can add annotations into your source code by using the Annotations view or by adding the annotation directly in the Java™ editor.

Procedure

  1. Determine which annotations to add to your web application.
    Table 1. Annotations supported by Java EE 5 web applications. This table describes common, EJB, and web service annotations for use in web applications.
    Annotation type Annotation Description
    Common annotations @Resource The Resource annotation declares a reference to a resource that is required by the application.
    @Resources The Resources annotation declares multiple resources declarations.
    @DeclaresRoles The DeclaresRoles annotation specifies the security roles for the application.
    @RunAs The RunAs annotation specifies the role of the application during run time.
    @PostConstruct The PostConstruct annotation specifies the container that will start after resource injection is complete to perform any initialization.
    @PreDestroy The PreDestroy annotation is used to signal that the instance is in being removed by the container.
    EJBs @PersistenceContext The PersistenceContext annotation specifies the container managed entity context.
    @PersistenceContexts The PersistenceContexts annotation declares multiple @PersistenceContext annotations.
    @PersistenceUnit The PersistenceUnit annotation specifies a reference to an entity manager factory for use with EJBs.
    @PersistenceUnits The PersistenceUnits annotation declares multiple @PersistenceUnit annotations.
    Web services @WebServiceRef The WebServiceRef annotation specifies a reference to a web service within a web application.
    @WebServicesRefs The WebServicesRefs annotation declares multiple @WebServiceRef annotations.
  2. Add annotations in the source code by using the Annotations view or by adding the annotation directly in the Java editor.

What to do next

For more information about web tier resource injection, refer to: