EJB Annotations: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(19 intermediate revisions by the same user not shown)
Line 9: Line 9:
=@javax.ejb.EJB=
=@javax.ejb.EJB=


Used to request the container to inject a reference to an EJB.
{{Internal|@javax.ejb.EJB|@javax.ejb.EJB}}


<pre>
=@javax.annotation.Resource=
@EJB
 
private MyBusinessInterface myEjb;
{{Internal|@javax.annotation.Resource|@javax.annotation.Resource}}
</pre>
 
=@javax.ejb.Remote=


The @EJB annotation is processed as follows:
See {{Internal|EJB_Concepts#Local_and_Remote_Interfaces|EJB Concepts - Local and Remote Interfaces}}
* The JAR of the referencing bean is searched for an EJB with the interface used in the @EJB injection. If there are more than one EJB that publishes the same business interface, an exception is thrown, otherwise the matching bean is returned.
* The EAR of the deployment is searched. If there are duplicates, an exception is thrown, otherwise the matching bean is returned.
* Search globally in the JBoss instance runtime for an EJB of that interface. If there are duplicates, an exception is thrown, otherwise the matching bean is returned.


==beanName==
=@javax.ejb.Local=


The value optionally specified here corresponds to <ejb-link>. If beanName is defined, the algorithm described above is used, except that beanName is used as key in the search. The "#" syntax allows to put a relative path to a jar in the EAR where the EJB you are referencing is located. More details in the EJB 3.1 specification.
See {{Internal|EJB_Concepts#Local_and_Remote_Interfaces|EJB Concepts - Local and Remote Interfaces}}


==mappedName==
=@javax.annotation.PostConstruct=


The annotation has a "mappedName()" attribute. The specification designates it as vendor specific metadata. JBoss uses this metadata to specify the global JNDI name of the EJB. If mappedName() is specified, all other attributes (name, description, beanName, beanInterface, lookup) are ignored and this global JNDI name is used for binding.
The @PostConstruct annotation is used on a method that needs to be executed after dependency injection is done into the component. The method such annotated is understood to perform initialization of the component. Only one method per component can be annotated with this annotation. The annotated method must not have any parameters (except in the case of interceptors in which case it takes an <tt>InvocationContext</tt> instance. The method may be public, protected, package private or private, but not static. It can be declared final. If the method throws an unchecked exception, the component must not be put into service except in the case of EJBs where the EJB can handle exceptions.


<pre>
Used by:
@EJB(mappedName = "/somehting/something")
* [[MDB#.40PostConstruct|MDBs]]
private MyBusinessInterface myEjb;
* [[Singleton_Session_EJB#Lifecycle|Singleton sesion beans]]
</pre>


For a working example, see [[Session_EJB_and_Servlet_in_Different_Deployment_Units_on_Same_JBoss_Instance#.40EJB|Session EJB and Servlet in Different Deployment Units on Same JBoss Instance]].
=@javax.annotation.PreDestroy=


=@javax.annotation.Resource=
The @PreDestroy annotation is used on a method to be invoked by the container when it intents the remove the component instance. Such methods are typically used to release resources that the component has been holding. This annotation must be supported by all container managed components that support [[#.40javax.annotation.PostConstruct|@PostConstruct]]. The annotated method must not have any parameters (except in the case of interceptors in which case it takes an <tt>InvocationContext</tt> instance. The method may be public, protected, package private or private, but not static.
 
Used by:
* [[MDB#.40PreDestroy|MDBs]]
* [[Singleton_Session_EJB#Lifecycle|Singleton sesion beans]]
 
=Annotations=
 
* [[@javax.persistence.PersistenceUnit|@javax.persistence.PersistenceUnit]]
* [[@javax.persistence.PersistenceContext|@javax.persistence.PersistenceContext]]
* [[@javax.ejb.TransactionAttribute|@javax.ejb.TransactionAttribute]]
* [[@javax.ejb.ApplicationException|@javax.ejb.ApplicationException]]
* [[@javax.ejb.Startup|@javax.ejb.Startup]]
* [[@javax.ejb.DependsOn|@javax.ejb.DependsOn]]
* [[@javax.ejb.Lock|@javax.ejb.Lock]]
* [[@javax.inject.Inject|@javax.inject.Inject]]
* [[@javax.ejb.Schedule|@javax.ejb.Schedule]]
* [[@javax.ejb.Schedules|@javax.ejb.Schedules]]
* [[@javax.ejb.Timeout|@javax.ejb.Timeout]]

Latest revision as of 15:07, 28 September 2017

Internal

Overview

If an XML deployment descriptor containing the same semantics exists, the XML content overrides the annotation.

@javax.ejb.EJB

@javax.ejb.EJB

@javax.annotation.Resource

@javax.annotation.Resource

@javax.ejb.Remote

See

EJB Concepts - Local and Remote Interfaces

@javax.ejb.Local

See

EJB Concepts - Local and Remote Interfaces

@javax.annotation.PostConstruct

The @PostConstruct annotation is used on a method that needs to be executed after dependency injection is done into the component. The method such annotated is understood to perform initialization of the component. Only one method per component can be annotated with this annotation. The annotated method must not have any parameters (except in the case of interceptors in which case it takes an InvocationContext instance. The method may be public, protected, package private or private, but not static. It can be declared final. If the method throws an unchecked exception, the component must not be put into service except in the case of EJBs where the EJB can handle exceptions.

Used by:

@javax.annotation.PreDestroy

The @PreDestroy annotation is used on a method to be invoked by the container when it intents the remove the component instance. Such methods are typically used to release resources that the component has been holding. This annotation must be supported by all container managed components that support @PostConstruct. The annotated method must not have any parameters (except in the case of interceptors in which case it takes an InvocationContext instance. The method may be public, protected, package private or private, but not static.

Used by:

Annotations