47

I don’t see any difference between two ways, @Qualifier is always used with @Autowired.

@Autowired
@Qualifier("alpha")

VS

@Resource(name="alpha")

Anyone could let me know the difference? Thanks!

卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130
  • possible duplicate of [@Resource vs @Autowired](http://stackoverflow.com/questions/4093504/resource-vs-autowired) – skaffman Feb 02 '12 at 09:24

3 Answers3

85

@Autowired can be used alone . If it is used alone , it will be wired by type . So problems arises if more than one bean of the same type are declared in the container as @Autowired does not know which beans to use to inject. As a result , use @Qualifier together with @Autowired to clarify which beans to be actually wired by specifying the bean name (wired by name)

@Resource is wired by name too . So if @Autowired is used together with @Qualifier , it is the same as the @Resource.

The difference are that @Autowired and @Qualifier are the spring annotation while @Resource is the standard java annotation (from JSR-250) . Besides , @Resource only supports for fields and setter injection while @Autowired supports fields , setter ,constructors and multi-argument methods injection.

It is suggested to use @Resource for fields and setter injection. Stick with @Qualifier and @Autowired for constructor or a multi-argument method injection.

See this:

If you intend to express annotation-driven injection by name, do not primarily use @Autowired - even if is technically capable of referring to a bean name through @Qualifier values. Instead, prefer the JSR-250 @Resource annotation which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.

Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • Where does it actually say that? I see that it says to use it if you want to autowire by name, I don't see where it's recommended that resource is preferred (and it won't actually work under some circumstances). – Dave Newton Feb 02 '12 at 03:04
  • Updated .It is said in the Tips section in the 3.11.3 – Ken Chan Feb 02 '12 at 03:11
5

I was facing some issues with @Autowired and then started using @Qualifier and I was finally able to find out the when to use @Autowired with @Qualifier when multiple beans of same type are defined.

Suppose you define 2 beans of same type but different values :

<bean id="appContext1" class="com.context.AppContext">
     <constructor-arg value="abc" />
<bean/>
<bean id="appContext2" class="com.context.AppContext">
     <constructor-arg value="ABC" />
<bean/>

Then if you just are trying to use @Autowire, then you have to use the same variable name as of the bean name else it will give error as multiple types found.

@Autowired
AppContext appContext;

For the above use case you have to use Qualifier.

@Autowired
@Qualifier("appContext1")
AppContext appContext;

Instead, if you use the variable name same as bean name, you can eliminate the use of @Qualifier.

@Autowired
AppContext appContext1;

I was always using the variable name same as bean name, but accidentally had some other variable name and faced this issue.

Let me know if there are any doubts.

3

@Autowired is old school Spring. @Resource is the Java EE CDI standard. Spring handles both (as well as @Inject, which is very similar) and does pretty much the same thing in both situations. I would recommend @Resource, @Autowired was made prior to the standard and seems to be supported mostly for backward compatibility.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Nialscorva
  • 2,924
  • 2
  • 15
  • 16