aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Difference between @EJB and @Resource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Difference between @EJB and @Resource" Watch "Difference between @EJB and @Resource" New topic
Author

Difference between @EJB and @Resource

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Guys,

What are the major differences between @EJB and @Resource annotations? One is that with @Resource, you can inject any object registered in the containers ENC, but with @EJB, you can only inject other EJB's. So what could be the other differences? What is the use of @Inject annotation when these two are more than sufficient?


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi Jothi

In my opinion the Resource annotation is used to reference an external resource (e.g. JMS message Destinations, EJBContext, and many many more).
The @EJB is limited to (enterprise) beans (Session & MDB). As far as I know you can not inject a bean by with the Resource annotation.So one is for beans and the other for the rest (resources).


Regards,
M


SCJP, SCJD, SCWCD, OCPJBCD
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

I do not think so. With the @Resource annotation, you can inject a resource as well as a bean. Can this resource be a POJO? or should it be a resource that is registered in the containers Initial Context?
Paul Kay
Greenhorn

Joined: Jan 04, 2009
Posts: 6
Hey Jothi,

You are quite right there.

@Resource can inject a resource as well as a bean.
The resource can not be an 'unregistered' POJO.
You can inject registered POJOs. for eg, JSF managed beans.

You would be able to inject 'any' POJOs in the future release(most probably). Thats really gonna help a lot.
Anything registered in the JNDI and managed by the container can be injected. ( Container needs to create it )

@EJB can only inject local and remote beans.
@Resource can be used anywhere for eg. web tier to inject resources.
Interestingly, EJBs can also be injected into Application clients in ACC 'static' ally.

@Inject is also for DI. Its getting amusing now, isnt it?

OK, @Inject is like generic and takes only the jndiName whereas @Resource and @EJB are more specific and more configurable with attributes like sharable, resourceType , authenticationType etc.

@Resource can be used at class level whereas @Inject and @EJB are method and field level.

Hope this helps you.
Wish you a happy and successful New Year.

- Paul


Look at us ... look at what they make you give ...

SCJP 5, SCWCD 5, SCBCD 5, SCEA 5, SCDJWS 5
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Good. Thanks for the reply. To double confirm it, I'm going to check this in my JBoss.

Wish you a happy and a successful new year too.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Paul Kay wrote:Hey Jothi,

You are quite right there.

@Resource can inject a resource as well as a bean.
The resource can not be an 'unregistered' POJO.
You can inject registered POJOs. for eg, JSF managed beans.

You would be able to inject 'any' POJOs in the future release(most probably). Thats really gonna help a lot.
Anything registered in the JNDI and managed by the container can be injected. ( Container needs to create it )

@EJB can only inject local and remote beans.
@Resource can be used anywhere for eg. web tier to inject resources.
Interestingly, EJBs can also be injected into Application clients in ACC 'static' ally.

@Inject is also for DI. Its getting amusing now, isnt it?

OK, @Inject is like generic and takes only the jndiName whereas @Resource and @EJB are more specific and more configurable with attributes like sharable, resourceType , authenticationType etc.

@Resource can be used at class level whereas @Inject and @EJB are method and field level.

Hope this helps you.
Wish you a happy and successful New Year.

- Paul



Injecting a POJO is something that I would expect in EJB 3.1. I think it's already quite late to introduce such a feature in EJB.
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi guys

In the "EJB Core Contracts and Requirements" on the section 16.5.1.1. I found :

The Bean Provider uses the EJB annotation to annotate a field or setter property method of the bean
class as a target for the injection of an EJB reference. The reference may be to a session bean’s business
interface or to the local home interface or remote home interface of a session bean or entity bean.


In my understanding this means if you need to inject a (managed) bean you need to use the @EJB annotation.

I also do some experiments and I fail when I try to inject a bean with @Resource.

And now comes the interesting part.
In the "EJB in action" from Manning on section 5.2.1 I found :

....However the (Resource) annotation can be also used for e-mails servers resources, environmennts entries, Orb references or even for EJB instances.

This affirmation is unfortunately wrong - in my opinion.

A also search in the "Enterprise JavaBeans 3.0" from O'Reilly but there is also no obvious reference on this feature.

So If I put all of this together, I don't think that is possible to inject a (managed) bean using the @Resource.

What you guys think about ?

Regards,
M
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi Paul,

I don't think that @Inject is EJB standard - or at least I can not find it the standard API ? Can you provide the package name also ?


Regards,
M
Paul Kay
Greenhorn

Joined: Jan 04, 2009
Posts: 6
Hey Mihai,

You are absolutely right.

@Inject used to be javax.ejb.Inject before standardization of spec. They voted it out in favor of the more refined @Resource and moved it to the javax.annotation package.

You may not find any reference using @Resource for injecting a bean, just because you have @EJB particularly for that.

Incidentally, you can also use @Inject for injecting a bean.

@Inject is the most basic DI annotation, forming the basis of IoC. Most AOP/IoC implementations support @Inject along with other more specific DI annotations.

Kindly keep us updated about your valuable findings in the regard; along with your environment and attribute values, if possible.

And have a fantastic year ahead.

- Paul

 
 
subject: Difference between @EJB and @Resource