Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the @EJBs annotation do?

I know roughly what this construction does: it creates a SomeType EJB and injects the object into another EJB.

 @EJB(name="name1")  SomeType someVariable 

Now I have a class which starts like this: (I give all class-level annotations, even though I think only the @EJBs is relevant)

@Remote(SomeClass.class) @Stateless(name="someName") @EJBs({@EJB(name="name1",beanInterface=Type1.class),        @EJB(name="name2",beanInterface=Type2.class)}) @TransactionAttribute(TransactionAttributeType.REQUIRED) @TransactionManagement(TransactionManagementType.CONTAINER) public class X extends Y{    //code 

What do the @EJB s do here? They probably get or create the "name1" ... objects from JNDI, but where do they put the result? I don't see a .lookup call anywhere near, but the codebase is huge so I'm not very sure about this.

Bonus question: I presume the two @Transaction annotations simply repeat defaults?

UPDATE: Multiple persons claimed at this point that @EJBs is a proprietary extension. It is not. It is a core part of java EE5. See the JavaDoc for details.. It is simply a container for the individual @EJB annotations.

I believe everyone who claims these EJB annotations do a lookup. I just want to know what happens with the result of this lookup.

like image 663
hyperman Avatar asked Sep 10 '12 09:09

hyperman


People also ask

What is the use of @EJB annotation?

The purpose of having annotations is to attach additional information in the class or a meta-data of a class within its source code. In EJB 3.0, annotations are used to describe configuration meta-data in EJB classes.

What is the difference between @EJB and @inject?

@Inject is more general than EJB and is part of CDI specification. So if you want to use @Inject, you need an implementation of it in your server. For POJOs (not EJBs) you have to use @Inject.

What are the different types of annotations used in EJB?

Component-defining annotations for EJBs include: @Stateless: Component-defining annotation for a stateless session bean. @Stateful: Component-defining annotation for a stateful session bean. @MessageDriven: Component-defining annotation for a message driven bean.

What is @local annotation in EJB?

javax.ejb The Local annotation is applied to the session bean class or local business interface to designate a local interface of the bean. When used on the bean class, declares the local business interface(s) for a session bean. When used on an interface, designates that interface as a local business interface.


1 Answers

The @EJB annotation (and @Resource, @WebServiceRef, etc.) serves two purposes:

  1. It declares a reference in the component namespace. For example, @EJB(name="myEJB") creates a reference java:comp/env/myEJB. If you annotate a field and do not specify a name, then it creates a reference java:comp/env/com.example.MyClass/myField.
  2. If the annotation is declared on a field or setter method, then the container performs injection when the component is created.

How the reference is resolved varies, independent of whether the reference is being resolved for a lookup("java:comp/env/myEJB") or due to injection:

  1. If EE 6+ is used, the lookup attribute requires a JNDI lookup to resolve the target.
  2. Some application servers support mappedName, which is specified to be vendor specific. This is usually implemented by performing a lookup.
  3. Application servers support bindings at deployment time. This is usually implemented by performing a lookup.
  4. If no other binding information is provided and the bean interface (beanInterface or the field type) is only implemented by a single EJB in the application, then the EJB specification requires that it fall back to that.
  5. If no other binding information is provided and #4 cannot work, some application servers will attempt to perform a lookup in the server namespace based on the ref name (for example, java:comp/env/myEJB might cause a lookup of myEJB in the server namespace).
like image 129
Brett Kail Avatar answered Sep 30 '22 19:09

Brett Kail