@Resource vs @Inject

2,814 views
Skip to first unread message

Fabrizio Giudici

unread,
Nov 25, 2009, 3:25:05 AM11/25/09
to java...@googlegroups.com
Hi.

Can somebody please quickly recap the difference between @Resource and
@Inject from JSR330? Afaik JSR330 has been definitely approved. My need
is to use an annotation to perform dependency injection that is the most
widely accepted - I'm using @Resource for a tiny, specifically focused
resource injection library and would like to have the resulting
annotated code to be as much as compatible with other injection
frameworks as possible (at wide range: JSE, JEE etc...). AFAIK @Inject
will be used as a standard, but I'd like to have this confirmed. THanks.

--
Fabrizio Giudici - Java Architect, Project Manager
Tidalwave s.a.s. - "We make Java work. Everywhere."
weblogs.java.net/blog/fabriziogiudici - www.tidalwave.it/people
Fabrizio...@tidalwave.it

Stuart McCulloch

unread,
Nov 25, 2009, 4:12:37 AM11/25/09
to java...@googlegroups.com
2009/11/25 Fabrizio Giudici <fabrizio...@tidalwave.it>
Hi.

Can somebody please quickly recap the difference between @Resource and
@Inject from JSR330? Afaik JSR330 has been definitely approved. My need
is to use an annotation to perform dependency injection that is the most
widely accepted - I'm using @Resource for a tiny, specifically focused
resource injection library and would like to have the resulting
annotated code to be as much as compatible with other injection
frameworks as possible (at wide range: JSE, JEE etc...). AFAIK @Inject
will be used as a standard, but I'd like to have this confirmed. THanks.

[ caveat: I help out with Guice, so the following opinion might be biased... ]

IMHO @Resource has extra semantics over and above simple injection, from javadoc:

   "The Resource annotation marks a resource that is needed by the application."

@Resource is implicitly tied with JNDI resources, and what's more the exact meaning
behind it can change depending on whether it annotates a method/field member, or a
class. If your library is definitely targeting JNDI injection rather than general injection
then probably @Resource is the one to choose.

On the other hand @Inject simply marks out members of a class that can be injected:

   "Identifies injectable constructors, methods, and fields."

which makes it better for general injection - see also Bob's comment at the end of:

   http://code.google.com/p/atinject/issues/detail?id=15#c15

BTW, the javax.inject API is already available on central along with src and javadoc:

   http://repo1.maven.org/maven2/javax/inject/javax.inject/1/javax.inject-1.jar

However, if you want something that's definitely supported by legacy/existing systems
then @Resource might have more coverage - although I expect that to change over the
next few years as people and frameworks starting picking up @Inject...
 
Or you could always support both annotations, as they're not mutually exclusive.

--
Fabrizio Giudici - Java Architect, Project Manager
Tidalwave s.a.s. - "We make Java work. Everywhere."
weblogs.java.net/blog/fabriziogiudici - www.tidalwave.it/people
Fabrizio...@tidalwave.it

--

You received this message because you are subscribed to the Google Groups "The Java Posse" group.
To post to this group, send email to java...@googlegroups.com.
To unsubscribe from this group, send email to javaposse+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.





--
Cheers, Stuart

Fabrizio Giudici

unread,
Nov 25, 2009, 4:38:44 AM11/25/09
to java...@googlegroups.com
Stuart McCulloch wrote:
>
>
>
> Or you could always support both annotations, as they're not mutually
> exclusive.
>
Great. You also answered to a couple of questions that I hadn't put yet ;-)

AlexisMP

unread,
Nov 25, 2009, 5:32:05 AM11/25/09
to java...@googlegroups.com, java...@googlegroups.com
I think it's fair to say that in a JavaEE 6 environment @Inject really
only makes sense in the context of CDI (jsr299).

Without CDI @Resource remains a (loosely typed) solution which Now
also applies to the new javax.annotations.ManagedBean.

In GlassFish v3, @Resource is also used to inject OSGi declarative
services in regular JavaEE artiacts.

--Alexis

On 25 nov. 2009, at 10:38, Fabrizio Giudici <fabrizio...@tidalwave.it

John Ament

unread,
Nov 25, 2009, 5:43:45 AM11/25/09
to The Java Posse
Its always sounded more like a scoping thing to me, and you kind of
sum it up as well. With @Resource, you're generally talking about
something that's been configured on the app server and sits somewhere
in the JNDI tree (typically in the app server global). @Inject though
is always local to the application itself. I can't recall any
examples with two standalone EAR files sharing resources via @Inject
(nor does it sound like a good idea). In our setup, we have a couple
of services deployed as standalone JAR files (reusable logic that many
of our apps all use), these can be injected via @EJB so I anticipate
them being able to be injected via @Inject if we move to CDI.

On Nov 25, 5:32 am, AlexisMP <alexis...@gmail.com> wrote:
> I think it's fair to say that in a JavaEE 6 environment @Inject really  
> only makes sense in the context of CDI (jsr299).
>
> Without CDI @Resource remains a (loosely typed) solution which Now  
> also applies to the new javax.annotations.ManagedBean.
>
> In GlassFish v3, @Resource is also used to inject OSGi declarative  
> services in regular JavaEE artiacts.
>
> --Alexis
>
> On 25 nov. 2009, at 10:38, Fabrizio Giudici <fabrizio.giud...@tidalwave.it
>
>  > wrote:
> > Stuart McCulloch wrote:
>
> >> Or you could always support both annotations, as they're not mutually
> >> exclusive.
>
> > Great. You also answered to a couple of questions that I hadn't put  
> > yet ;-)
>
> > --
> > Fabrizio Giudici - Java Architect, Project Manager
> > Tidalwave s.a.s. - "We make Java work. Everywhere."
> > weblogs.java.net/blog/fabriziogiudici -www.tidalwave.it/people
> > Fabrizio.Giud...@tidalwave.it

Stuart McCulloch

unread,
Nov 25, 2009, 6:02:48 AM11/25/09
to java...@googlegroups.com
2009/11/25 John Ament <john.d...@gmail.com>
Its always sounded more like a scoping thing to me, and you kind of
sum it up as well.  With @Resource, you're generally talking about
something that's been configured on the app server and sits somewhere
in the JNDI tree (typically in the app server global).  @Inject though
is always local to the application itself.  I can't recall any
examples with two standalone EAR files sharing resources via @Inject
(nor does it sound like a good idea).

Strictly speaking @Inject just decorates members that can be injected.
It doesn't specify where the injected instances come from, or how they
are configured - which is why I said @Resource has extra semantics.

There's no reason why you couldn't have a system that injected shared
resources according to @Inject annotations (ie. using them as a marker)
along with a @Named annotation to name the JNDI resource.

And as I mentioned earlier, you could have @Resource and @Inject on
the same class member - probably overkill, but it would mean that the
component could be used in both JEE and JSE applications. *

So, basically @Resource says more about the injection than @Inject
(which is more general) - which one you choose depends on what you
expect to inject and what semantics you want to apply.

[ * this doesn't necessarily introduce any extra runtime dependencies,
  if you don't have the JSR-250 jar on your classpath you won't see the
  @Resource annotations, and vice versa for JSR-330 ]

For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.





--
Cheers, Stuart
Reply all
Reply to author
Forward
0 new messages