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

  • Diff between @Resource, @Autowired and @Required

    Dear all,

    I am new to spring annotation, and confused by the different between @Required, @Autowired and @Resource.

    From Spring reference we have these examples looks almost the same:

    For @Required:
    Code:
    public class SimpleMovieLister {
    
        private MovieFinder movieFinder;
    
        @Required
        public void setMovieFinder(MovieFinder movieFinder) {
            this.movieFinder = movieFinder;
        }
    
        // ...
    }
    For @Autowired
    Code:
    public class SimpleMovieLister {
    
        private MovieFinder movieFinder;
    
        @Autowired
        public void setMovieFinder(MovieFinder movieFinder) {
            this.movieFinder = movieFinder;
        }
    
        // ...
    }
    and for @Resource:
    Code:
    public class SimpleMovieLister {
    
        private MovieFinder movieFinder;
    
        @Resource
        public void setMovieFinder(MovieFinder movieFinder) {
            this.movieFinder = movieFinder;
        }
    }
    To my understanding:
    @Required: Must be set during bean configuration
    @Autowired: It is ok if the setter is not ready by configuration time, but it is only for Spring
    @Resource: No specifiied requirement, almost the same as Autowired expect its for general Java

    From Spring reference:
    A. between @Required and @autowired:
    This annotation(@Required) simply indicates that the affected bean property must be populated at configuration time.
    and
    By default, the autowiring will fail whenever zero candidate beans are available; the default behavior is to treat annotated methods, constructors, and fields as indicating required dependencies.

    and
    B. between @Resource and @autowried
    (@Resource) Similar to @Autowired, @Resource may fall back to standard bean type matches (i.e. find a primary type match instead of a specific named bean) as well as resolve well-known "resolvable dependencies": the BeanFactory interface, the ApplicationContext interface, the ResourceLoader interface, the ApplicationEventPublisher interface and the MessageSource interface. Note that this only applies to @Resource usage with no explicit name specified!

    So, if following are correct:
    from statement A:
    Code:
    @Required 
    public String setFoo(..);
    is the same as
    Code:
    @Autowried(required=true)
    public String setFoo(..);
    and from statement B:
    Code:
    @Autowried
    @Qualifier("foo")
    public String setFoo(..);
    is the same as
    Code:
    @Resource(name="foo")
    public String setFoo(..);

    and we have @autowired that able to behave the same as either @required or @resource, if corresponding setting apply?

    Thanks in advance
Working...
X