@Autowired优先按type注入,@Resource优先按name注入

在spring框架下使用注入annotation,应该用@Autowired还是@Resource?

基本上都差不多,但如果存在不同bean共享同一个java类的情况,则应该使用@Resource. 因为,

@Resource寻找bean的顺序

  Matches by Name

  Matches by Type

@Autowired寻找bean的顺序 

  Matches by Type      //如果存在不同Bean共享java类,就会出现NoUniqueBean异常

  Matches by Name

更多请见
http://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.