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 aValidatorobject for the default bean validation objectValidatorFactoryto be injected. -
javax.validation.ValidatorFactory: A factory class for returning initializedValidatorinstances. The bean that implements this interface enables the default bean validationValidatorFactoryobject 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 |
|---|---|---|
|
|
Resource |
|
|
|
Resource |
|
|
|
Resource |
|
|
|
Resource |
|
|
|
CDI bean |
|
|
|
CDI bean |
|
|
|
CDI bean |
|
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;
...
}
