Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring, @Autowired, @Resource and <property>

Tags:

java

spring

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

like image 993
willcodejavaforfood Avatar asked Nov 05 '10 09:11

willcodejavaforfood


People also ask

What is difference between @autowired and @resource in Spring?

@Autowired in combination with @Qualifier also autowires by name. The main difference is is that @Autowired is a spring annotation whereas @Resource is specified by the JSR-250. So the latter is part of normal java where as @Autowired is only available by spring.

What does @resource mean in Spring?

The @Resource annotation in spring performs the autowiring functionality. This annotation follows the autowire=byName semantics in the XML based configuration i.e. it takes the name attribute for the injection.

What is the use of @resource annotation in Spring?

We can resolve dependencies by field injection by annotating an instance variable with the @Resource annotation. This configuration will resolve dependencies using the match-by-name execution path. We must define the bean namedFile in the ApplicationContextTestResourceNameType application context.

What is the use of @resource annotation?

The @Resource annotation is used to identify a class, field, or method that upon initialization, the resource will be injected. For a class-based @Resource, the "resource is looked up by the application at runtime".


2 Answers

@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.

like image 170
Sean Patrick Floyd Avatar answered Sep 20 '22 22:09

Sean Patrick Floyd


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?)

like image 32
Stephen C Avatar answered Sep 18 '22 22:09

Stephen C