• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between @EJB and @Resource

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
reply
    Bookmark Topic Watch Topic
  • New Topic