# The Java EE 7 Tutorial

# 25.4 Using Predefined Beans in CDI Applications

Java EE provides predefined beans that implement the following interfaces.

- `javax.transaction.UserTransaction`: A Java Transaction API (JTA) user transaction.

- `java.security.Principal`: The abstract notion of a principal, which represents any entity, such as an individual, a corporation, or a login ID. Whenever the injected principal is accessed, it always represents the identity of the current caller. For example, a principal is injected into a field at initialization. Later, a method that uses the injected principal is called on the object into which the principal was injected. In this situation, the injected principal represents the identity of the current caller when the method is run.

- `javax.validation.Validator`: A validator for bean instances. The bean that implements this interface enables a `Validator` object for the default bean validation object `ValidatorFactory` to be injected.

- `javax.validation.ValidatorFactory`: A factory class for returning initialized `Validator` instances. The bean that implements this interface enables the default bean validation `ValidatorFactory` object to be injected.

- `javax.servlet.http.HttpServletRequest`: An HTTP request from a client. The bean that implements this interface enables a servlet to obtain all the details of a request.

- `javax.servlet.http.HttpSession`: An HTTP session between a client and a server. The bean that implements this interface enables a servlet to access information about a session and to bind objects to a session.

- `javax.servlet.ServletContext`: A context object that servlets can use to communicate with the servlet container.

To inject a predefined bean, create an injection point to obtain an instance of the bean by using the `javax.annotation.Resource` annotation for resources or the `javax.inject.Inject` annotation for CDI beans. For the bean type, specify the class name of the interface the bean implements.

Table 25-1 Injection of Predefined Beans

Predefined Bean | Resource or CDI Bean | Injection Example
--- | --- | ---
`UserTransaction` | Resource | `@Resource UserTransaction transaction;`
`Principal` | Resource | `@Resource Principal principal;`
`Validator` | Resource | `@Resource Validator validator;`
`ValidatorFactory` | Resource | `@Resource ValidatorFactory factory;`
`HttpServletRequest` | CDI bean | `@Inject HttpServletRequest req;`
`HttpSession` | CDI bean | `@Inject HttpSession session;`
`ServletContext` | CDI bean | `@Inject ServletContext context;`

Predefined beans are injected with dependent scope and the predefined default qualifier `@Default`.

For more information about injecting resources, see Resource Injection.

The following code snippet shows how to use the `@Resource` and `@Inject` annotations to inject predefined beans. This code snippet injects a user transaction and a context object into the servlet class `TransactionServlet`. The user transaction is an instance of the predefined bean that implements the `javax.transaction.UserTransaction` interface. The context object is an instance of the predefined bean that implements the `javax.servlet.ServletContext` interface.

    import javax.annotation.Resource;
    import javax.inject.Inject;
    import javax.servlet.http.HttpServlet;
    import javax.transaction.UserTransaction;
    ...
    public class TransactionServlet extends HttpServlet {
        @Resource UserTransaction transaction;
        @Inject ServletContext context;
        ...
    }