7

This week I've been trying to learn Spring, JBoss, Maven, JPA and Hibernate and I've had a lot of fun with it. I am somewhat confused over the many different ways to inject resources in a class though. Until this week I was not even aware that you could inject resources in any other way than using the <property> tag in your Spring XML configuration.

<bean id="catalogService" class="com.idbs.omics.catalog.service.CatalogService">
    <property name="termDao" ref="termDao"></property>
</bean>

When I started experimenting with JPA I encountered @PersistenceContext, but that seems to be a special case so fair enough. Then I started reading up on Spring's testing framework and I saw the first example that used @Resource(name="catalogService") and then in a Web Service example @Autowired crashed the party!

**The Question!**

So what is the difference between all these and is there right and wrong situation to use them in? I guess I'm looking for a best practice here.

Cheers all

willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111

2 Answers2

13

@Autowired, @Resource and @Inject

I think the part of the Spring Reference that you need to read is

3.9 Annotation-based container configuration


There are many sets of similar annotations. Often, there is a Spring and a non-Spring version that do the same thing. Spring tries to embrace standards whenever they are available, but often the Spring guys came up with their own ideas before a standard appeared. Example: Spring supports its own @Autowired annotation, but also the new @Inject annotation from JSR-330, as well as the JSR-250 @Resource annotation (all of which can be used to do the same thing).

The key concept here is that Spring doesn't force you to use its own code, but supports many different ways without coupling your application to Spring. (There are still a few annotations that have no non-Spring equivalent, like @Transactional, but if you want to, you can add that functionality via XML instead, so you can keep your application 100% Spring free and still use many convenience annotations and of course Spring wiring and lifecycle management behind the scenes.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • @duffymo *another person who can't distinguish "it's" from "its"* I can, usually, but I guess today it's a case of too little sleep and too much coffee. Thx for the edit. – Sean Patrick Floyd Nov 05 '10 at 10:19
  • No worries, seanizer. I should have also pointed out that your good post was spot on. An error in punctuation was all I could disagree with. – duffymo Nov 05 '10 at 11:16
  • *Also from Spring Reference:* JSR 330's @Inject annotation can be used in place of Spring's @Autowired in the examples below. @Inject does not have a required property unlike Spring's @Autowired annotation which has a required property to indicate if the value being injected is optional. This behavior is enabled automatically if you have the JSR 330 JAR on the classpath. – Rihards Mar 20 '11 at 17:03
  • 1
    There exists some other caveats here. `@Autowired` can not be used to inject non-typed fields such as collections out of the box. On the flip `@Resource` can not be used for multi-argument methods. – TechTrip Apr 04 '12 at 20:44
  • This course walks you through a lot of that: http://pluralsight.com/training/Courses/TableOfContents/spring-jpa-hibernate – bh5k Apr 11 '13 at 14:39
2

The other aspect of this is when to use annotations and when to use XML spring wiring files.

My view is that this is a trade-off between code readability and the ability to reconfigure.

  • If you use annotations, then you can see from just the source code what is wired to what. By contrast, if you use XML wiring files, you have two places to look.

  • If you use XML wiring files, you can make configuration changes by modifying (or with Maven overlaying) the XML wiring files. If you are bold, you can even do this on a deployed webapp. By contrast, changing the IoC wirings when annotations are used requires you to modify the Java files and recompile.

(Aside: in the case of the second bullet, the ideal situation would be to have a nice GUI-based tool for reconfiguring the wirings that could be run in either your development sandbox, or the deployed webapp folder. Does anyone know of such a tool?)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216