EJB 3.0 Metadata Annotations Reference
WARNING: | This information applies only to the tech preview of BEA's implementation of the Enterprise JavaBeans, Version 3.0, specification. The tech preview is packaged in a special version of WebLogic Server 9.2 Beta. To use the EJB 3.0 features of the tech preview, you must install the tech preview separately from any existing WebLogic Server 9.2 Beta installation. For details, see Installing the EJB 3.0 Tech Preview. |
The following topics provide reference information about the EJB 3.0 metadata annotations:
Overview of EJB 3.0 Annotations
The new EJB 3.0 programming model uses the JDK 5.0 metadata annotations feature in which you create an annotated EJB 3.0 bean file and then use the WebLogic compile tool weblogic.appc
(or its Ant equivalent wlappc
) to compile the bean file into a Java class file and generate the associated EJB artifacts, such as the required EJB interfaces and deployment descriptors.
The following sections provide reference information for the metadata annotations you can specify in the EJB bean file. Some of the annotations are in the javax.ejb
package, and are thus specific to EJBs; others are more common and are used by other J2EE components, and are thus in more generic packages, such as javax.annotation
.
Annotations for Stateless, Stateful, and Message-Driven Beans
This section provides reference information for the following annotations:
- javax.ejb.ActivationConfigProperty
- javax.ejb.ApplicationException
- javax.ejb.EJB
- javax.ejb.EJBs
- javax.ejb.Init
- javax.ejb.Local
- javax.ejb.LocalHome
- javax.ejb.MessageDriven
- javax.ejb.PostActivate
- javax.ejb.PrePassivate
- javax.ejb.Remote
- javax.ejb.RemoteHome
- javax.ejb.Remove
- javax.ejb.Stateful
- javax.ejb.Stateless
- javax.ejb.Timeout
- javax.ejb.TransactionAttribute
- javax.ejb.TransactionManagement
javax.ejb.ActivationConfigProperty
Description
Specifies properties used to configure a message-driven bean in its operational environment. This may include information about message acknowledgement modes, message selectors, expected destination or endpoint types, and so on.
This annotation is used only as a value to the activationConfig
attribute of the @
javax.ejb.MessageDriven
annotation.
Attributes
javax.ejb.ApplicationException
Description
Specifies that an exception is an application exception and that it should be reported to the client application directly, or unwrapped.
This annotation can be applied to both checked and unchecked exceptions.
Attributes
javax.ejb.EJB
Description
Specifies a dependency or reference to an EJB business or home interface.
You annotate a bean's instance variable with the @EJB
annotation to specify a dependence on another EJB. WebLogic Server automatically initializes the annotated variable with the reference to the EJB on which it depends; this is also called dependency injection. This initialization occurs before any of the bean's business methods are invoked and after the bean's EJBContext
is set.
You can also annotate a setter method in the bean class; in this case WebLogic Server uses the setter method itself when performing dependency injection. This is an alternative to instance variable dependency injection.
If you apply the annotation to a class, the annotation declares the EJB that the bean will look up at runtime.
Whether using variable or setter method injection, WebLogic Server determines the name of the referenced EJB by either the name or data type of the annotated instance variable or setter method parameter. If there is any ambiguity, you should use the attributes of the @EJB
annotation to explicitly name the dependent EJB.
Attributes
| |||||
javax.ejb.EJBs
Description
Specifies an array of @javax.ejb.EJB
annotations.
Attribute
javax.ejb.Init
Description
Specifies the correspondence of a method in the bean class with a create
METHOD
method for an adapted EJB 2.1 EJBHome
or EJBLocalHome
client view.
This annotation is used only in conjunction with stateful session beans, or those that have been annotated with the @javax.ejb.Stateful
class-level annotation,
The return type of a method annotated with the @javax.ejb.Init
annotation must be void
, and its parameter types must be exactly the same as those of the referenced create
METHOD
method or methods.
The @Init annotation is required only for stateful session beans that provide a Remote-Home or LocalHome interface. You must specify the name of the adapted create method of the Home or LocalHome interface, using the value
attribute, if there is any ambiguity.
Attributes
javax.ejb.Local
Description
Specifies the local interface or interfaces of a session bean. The local interface exposes business logic to local clients—those running in the same application as the EJB. It defines the business methods a local client can call.
You are required to specify this annotation if your bean class implements more than a single interface, not including the following:
This annotation applies only to stateless or stateful session beans.
Attributes
javax.ejb.LocalHome
Description
Specifies the local home interface of the bean class.
The local home interface provides methods that local clients—those running in the same application as the EJB—can use to create, remove, and in the case of an entity bean, find instances of the bean. The local home interface also has home methods—business logic that is not specific to a particular bean instance.
This attribute applies only to stateless and stateful session beans.
You typically specify this attribute only if you are going to provide an adapted EJB 2.1 component view of the EJB 3.0 bean. You can also use this annotation with bean classes that have been written to the EJB 2.1 APIs.
Attributes
javax.ejb.MessageDriven
Description
Specifies that the Enterprise JavaBean is a message-driven bean.
Attributes
Specifies the configuration of the message-driven bean in its operational environment. This may include information about message acknowledgement modes, message selectors, expected destination or endpoint types, and so on.
You specify activation configuration information using an Array of
@
javax.ejb.ActivationConfigProperty annotation, specify the property name and value.
| |||
javax.ejb.PostActivate
Description
Specifies the lifecycle callback method that signals that the EJB container has just reactivated the bean instance.
This annotation applies only to stateful session beans. Because the EJB container automatically maintains the conversational state of a stateful session bean instance when it is passivated, you do not need to specify this annotation for most stateful session beans. You only need to use this annotation, along with its partner @PrePassivate
, if you want to allow your stateful session bean to maintain the open resources that need to be closed prior to a bean instance's passivation and then reopened during the bean instance's activation.
Only one method in the bean class can be annotated with this annotation.
The method annotated with @PostActivate
must follow these requirements:
- The method must have a single parameter of data type
javax.interceptor.InvocationContext
- The return type of the method must be
void
. - The method must not throw a checked exception.
- The method may be
public
,protected
,package private
orprivate
. - The method must not be
static
. - The method may be
final
.
This annotation does not have any attributes.
javax.ejb.PrePassivate
Description
Specifies the lifecycle callback method that signals that the EJB container is about to passivate the bean instance.
This annotation applies only to stateful session beans. Because the EJB container automatically maintains the conversational state of a stateful session bean instance when it is passivated, you do not need to specify this annotation for most stateful session beans. You only need to use this annotation, along with its partner @PostActivate
, if you want to allow your stateful session bean to maintain the open resources that need to be closed prior to a bean instance's passivation and then reopened during the bean instance's activation.
Only one method in the bean class can be annotated with this annotation.
The method annotated with @PrePassivate
must follow these requirements:
- The method must have a single parameter of data type
javax.interceptor.InvocationContext
- The return type of the method must be
void
. - The method must not throw a checked exception.
- The method may be
public
,protected
,package private
orprivate
. - The method must not be
static
. - The method may be
final
.
This annotation does not have any attributes.
javax.ejb.Remote
Description
Specifies the remote interface or interfaces of a session bean. The remote interface exposes business logic to remote clients—clients running in a separate application from the EJB. It defines the business methods a remote client can call.
This annotation applies only to stateless or stateful session beans.
Attributes
javax.ejb.RemoteHome
Description
Specifies the remote home interface of the bean class.
The remote home interface provides methods that remote clients—those running in a separate application from the EJB—can use to create, remove, and find instances of the bean.
This attribute applies only to stateless and stateful session beans.
You typically specify this attribute only if you are going to provide an adapted EJB 2.1 component view of the EJB 3.0 bean. You can also use this annotation with bean classes that have been written to the EJB 2.1 APIs.
Attributes
javax.ejb.Remove
Description
Use the @javax.ejb.Remove
annotation to denote a remove method of a stateful session bean.
When the method completes, the EJB container will invoke the method annotated with the @
javax.annotation.PreDestroy
annotation, if any, and then destroy the stateful session bean.
Attributes
javax.ejb.Stateful
Description
Specifies that the Enterprise JavaBean is a stateful session bean.
Attributes
| |||||
javax.ejb.Stateless
Description
Specifies that the Enterprise JavaBean is a stateless session bean.
Attributes
| |||||
javax.ejb.Timeout
Description
Specifies the timeout method of the bean class.
This annotation makes it easy to program an EJB timer service in your bean class. The EJB timer service is an EJB-container provided service that allows you to create timers that schedule callbacks to occur when a timer object expires.
Previous to EJB 3.0, your bean class was required to implement javax.ejb.TimedObject
if you wanted to program the timer service. Additionally, your bean class had to include a method with the exact name ejbTimeout
. These requirements are relaxed in Version 3.0 of EJB. You no longer are required to implement the javax.ejb.TimedObject
interface, and you can name your timeout method anything you want, as long as you annotate it with the @Timeout
annotation. You can, however, continue to use the pre-3.0 way of programming the timer service if you want.
For details, see Programming the EJB Timer Service.
This annotation does not have any attributes.
javax.ejb.TransactionAttribute
Description
Specifies whether the EJB container invokes an EJB business method within a transaction context.
WARNING: | If you specify this annotation, you are also required to use the @TransactionManagement annotation to specify container-managed transaction demarcation. |
You can specify this annotation on either the bean class, or a particular method of the class that is also a method of the business interface. If specified at the bean class, the annotation applies to all applicable business interface methods of the class. If specified for a particular method, the annotation applies to that method only. If the annotation is specified at both the class and the method level, the method value overrides if the two disagree.
If you do not specify the @TransactionAttribute
annotation in your bean class, and the bean uses container managed transaction demarcation, the semantics of the REQUIRED transaction attribute are assumed.
Attributes
For details about these values, see the description of the trans-attribute element in the
Container-Managed Transactions Elements table.
|
javax.ejb.TransactionManagement
Description
Specifies the transaction management demarcation type of the session bean or message-driven bean.
A transaction is a unit of work that changes application state—whether on disk, in memory or in a database—that, once started, is completed entirely, or not at all. Transactions can be demarcated—started, and ended with a commit or rollback—by the EJB container, by bean code, or by client code. This annotation specifies whether the EJB container or the user-written bean code manages the demarcation of a transaction.
If you do not specify this annotation in your bean class, it is assumed that the bean has container-managed transaction demarcation.
For additional information about transactions, see Transaction Design and Management Options.
Attributes
Annotations Used to Configure Interceptors
This section provides reference information for the following annotations:
javax.interceptor.AroundInvoke
Description
Specifies the business method interceptor for either a bean class or an interceptor class.
You can annotate only one method in the bean class or interceptor class with the @AroundInvoke annotation; the method cannot be a business method of the bean class.
This annotation does not have any attributes.
javax.interceptor.ExcludeClassInterceptors
Description
Specifies that any class-level interceptors should not be invoked for the annotated method. This does not include default interceptors, whose invocation are excluded only with the @ExcludeDefaultInterceptors
annotation.
This annotation does not have any attributes.
javax.interceptor.ExcludeDefaultInterceptors
Description
Specifies that any defined default interceptors (which can be specified only in the EJB deployment descriptors, and not with annotations) should not be invoked.
If defined at the class-level, the default interceptors are never invoked for any of the bean's business methods. If defined at the method-level, the default interceptors are never invoked for the particular business method, but they are invoked for all other business methods that do not have the @ExludeDefaultInterceptors
annotation.
This annotation does not include any attributes.
javax.interceptor.Interceptors
Description
Specifies the interceptor classes that are associated with the bean class or method. An interceptor class is a class—distinct from the bean class itself—whose methods are invoked in response to business method invocations and/or lifecycle events on the bean.
The interceptor class can include both an business interceptor method (annotated with the @javax.interceptor.AroundInvoke
annotation) and lifecycle callback methods (annotated with the @javax.annotation.PostConstruct
, @javax.annotation.PreDestroy
, @javax.ejb.PostActivate
, and @javax.ejb.PrePassivate
annotations).
Any number of interceptor classes may be defined for a bean class. If more than one interceptor class is defined, they are invoked in the order they are specified in the annotation.
If the annotation is specified at the class-level, the interceptors apply to all business methods of the EJB. If specified at the method-level, the interceptors apply to just that method. You can specify the same interceptor class to more than one method of the bean class. By default, method-level interceptors are invoked after all applicable interceptors (default interceptors, class-level interceptors, and so on).
Attributes
Annotations Used to Interact With Entity Beans
This section provides reference information about the following annotations:
javax.persistence.PersistenceContext
Description
Specifies a dependency on a container-managed EntityManager
persistence context.
You use this annotation to interact with a 3.0 entity bean, typically by performing dependency injection into an EntityManager
instance.
The EntityManager
interface defines the methods that are used to interact with the persistence context. A persistence context is a set of entity instances; an entity is a lightweight persistent domain object. The EntityManager
API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities.
Attributes
javax.persistence.PersistenceContexts
Description
Specifies an array of @javax.persistence.PersistencContext
annotations.
Attributes
Specifies the array of |
javax.persistence.PersistenceUnit
Description
Specifies a dependency on an EntityManagerFactory
object.
You use this annotation to interact with a 3.0 entity bean, typically by performing dependency injection into an EntityManagerFactory
instance. You can then use the EntityManagerFactory
to create one or more EntityManager
instances. This annotation is similar to the @PersistenceContext
annotation, except that it gives you more control over the life of the EntityManager
because you create and destroy it yourself, rather than let the EJB container do it for you.
The EntityManager
interface defines the methods that are used to interact with the persistence context. A persistence context is a set of entity instances; an entity is a lightweight persistent domain object. The EntityManager
API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities.
Attributes
Refers to the name of the persistence unit as defined in the
persistence.xml file. This file is an XML file, located in the
META-INF directory of the EJB JAR file, that specifies the database used with the entity beans and specifies the default behavior of the
EntityManager .
If you set this attribute, the EJB container automatically deploys the referenced persistence unit and sets its JNDI name to its persistence unit name. Similarly, if you do not specify this attribute, but the name of the variable into which you are injecting the persistence context information is the same as the name of a persistence unit in the
persistence.xml file, then the EJB container again automatically deploys the persistence unit with its JNDI name equal to its unit name.
|
javax.persistence.PersistenceUnits
Description
Specifies an array of @javax.persistence.PersistenceUnit
annotations.
Attributes
Specifies the array of |
Standard JDK Annotations Used By EJB 3.0
This section provides reference information about the following annotations:
javax.annotation.PostConstruct
Description
Specifies the lifecycle callback method that the EJB container should execute before the first business method invocation and after dependency injection is done to perform any initialization.
You must specify a @PostConstruct
method in any bean class that includes dependency injection.
Only one method in the bean class can be annotated with this annotation.
The method annotated with @PostConstruct
must follow these requirements:
- The method must have a single parameter of data type
javax.interceptor.InvocationContext
- The return type of the method must be
void
. - The method must not throw a checked exception.
- The method may be
public
,protected
,package private
orprivate
. - The method must not be
static
. - The method may be
final
.
This annotation does not have any attributes.
javax.annotation.PreDestroy
Description
Specifies the lifecycle callback method that signals that the bean class instance is about to be destroyed by the EJB container. You typically apply this annotation to methods that release resources that the bean class has been holding.
Only one method in the bean class can be annotated with this annotation.
The method annotated with @PreDestroy
must follow these requirements:
- The method must have a single parameter of data type
javax.interceptor.InvocationContext
- The return type of the method must be
void
. - The method must not throw a checked exception.
- The method may be
public
,protected
,package private
orprivate
. - The method must not be
static
. - The method may be
final
.
This annotation does not have any attributes.
javax.annotation.Resource
Description
Specifies a dependence on an external resource, such as a JDBC data source or a JMS destination or connection factory.
If you specify the annotation on a field or method, the EJB container injects an instance of the requested resource into the bean when the bean is initialized. If you apply the annotation to a class, the annotation declares a resource that the bean will look up at runtime.
Attributes
If you apply the
@Resource annotation to a field, the default value of the
name attribute is the field name, qualified by the class name. If you apply it to a method, the default value is the JavaBeans property name corresponding to the method, qualified by the class name. If you apply the annotation to class, there is no default value and thus you are required to specify the attribute.
| |||||
If you apply the
@Resource annotation to a field, the default value of the
type attribute is the type of the field. If you apply it to a method, the default is the type of the JavaBeans property. If you apply it to a class, there is no default value and thus you are required to specify this attribute.
| |||||
| |||||
javax.annotation.Resources
Description
Specifies an array of @Resource
annotations.
Attributes
Standard Security-Related JDK Annotations Used by EJB 3.0
This section provides reference information about the following annotations:
javax.annotation.security.DeclareRoles
Description
Defines the security roles that will be used in the EJB.
You typically use this annotation to define roles that can be tested from within the methods of the annotated class, such as using the isUserInRole
method. You can also use the annotation to explicitly declare roles that are implicitly declared if you use the @RolesAllowed
annotation on the class or a method of the class.
You create security roles in WebLogic Server using the Administration Console. For details, see Manage Security Roles.
Attributes
javax.annotation.security.DenyAll
Description
Specifies that no security role is allowed to access the annotated method, or in other words, the method is excluded from execution in the EJB container.
This annotation does not have any attributes.
javax.annotation.security.PermitAll
Description
Specifies that all security roles currently defined for WebLogic Server are allowed to access the annotated method.
This annotation does not have any attributes.
javax.annotation.security.RolesAllowed
Description
Specifies the list of security roles that are allowed to access methods in the EJB.
If you specify it at the class-level, then it applies to all methods in the bean class. If you specify it at the method-level, then it only applies to that method. If you specify the annotation at both the class- and method-level, the method value overrides the class value.
You create security roles in WebLogic Server using the Administration Console. For details, see Manage Security Roles.
Attributes
javax.annotation.security.RunAs
Description
Specifies the security role which actually executes the EJB in the EJB container.
The security role must exist in the WebLogic Server security realm and map to a user or group. For details, see Manage Security Roles.