Difference between @Qualifier and @Resource

前端 未结 3 625
谎友^
谎友^ 2020-12-07 13:45

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

@Autowired
@Qualifier(\"alpha\")

相关标签:
3条回答
  • 2020-12-07 14:24

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

    0 讨论(0)
  • 2020-12-07 14:35

    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.

    0 讨论(0)
  • 2020-12-07 14:36

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

    0 讨论(0)
提交回复
热议问题