Announcement Announcement Module
Collapse
No announcement yet.
Autowired and Resource difference Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowired and Resource difference

    Hello,

    is there any difference in using @Autowired vs. @Resource?

  • #2
    @Autowired wires per type and
    @Resource wires per bean name

    see chapter 3.11.1 of Reference Doc:
    http://static.springframework.org/sp...red-annotation

    and chapetr 3.11.3 of Reference Doc:
    http://static.springframework.org/sp...rce-annotation

    Comment


    • #3
      @Autowired wires per type and
      @Resource wires per bean name
      Well not fully true.

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

      Comment


      • #4
        Originally posted by mdeinum View Post
        Well not fully true.

        @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.
        So what is the general preference for injecting a bean with the same name as the property ? It seems we could almost toss a coin.

        Comment


        • #5
          From what I've read, using @Autowired with the @Qualifier.

          Comment


          • #6
            replying to old post, but it may help some one like me,

            The below is the information about this which i found in Spring reference docs

            http://static.springsource.org/sprin...ion-qualifiers

            Tip

            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.

            As a specific consequence of this semantic difference, beans which are themselves defined as a collection or map type cannot be injected via @Autowired since type matching is not properly applicable to them. Use @Resource for such beans, referring to the specific collection/map bean by unique name.

            Note: In contrast to @Autowired which is applicable to fields, constructors and multi-argument methods (allowing for narrowing through qualifier annotations at the parameter level), @Resource is only supported for fields and bean property setter methods with a single argument. As a consequence, stick with qualifiers if your injection target is a constructor or a multi-argument method.

            So for my case I decided to use @Resource.

            Comment


            • #7
              Originally posted by shiva.forums View Post
              Tip

              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.

              As a specific consequence of this semantic difference, beans which are themselves defined as a collection or map type cannot be injected via @Autowired since type matching is not properly applicable to them. Use @Resource for such beans, referring to the specific collection/map bean by unique name.

              Note: In contrast to @Autowired which is applicable to fields, constructors and multi-argument methods (allowing for narrowing through qualifier annotations at the parameter level), @Resource is only supported for fields and bean property setter methods with a single argument. As a consequence, stick with qualifiers if your injection target is a constructor or a multi-argument method.

              So for my case I decided to use @Resource.
              A very usefull info. Thanks a lot shiva.forums!

              Comment


              • #8
                hey,
                i have no understand that what are you asking about i think they have worked same way so may b not because you have not explain

                Comment

                Working...
                X