@Autowired 默认是按照类去匹配,配合 @Qualifier 指定按照名称去装配 bean。
常见用法:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import blog.service.ArticleService; import blog.service.TagService; import blog.service.TypeService; @Controller public class TestController { //成员属性字段使用 @Autowired,无需字段的 set 方法 @Autowired private TypeService typeService; //set 方法使用 @Autowired private ArticleService articleService; @Autowired public void setArticleService(ArticleService articleService) { this.articleService = articleService; } //构造方法使用 @Autowired private TagService tagService; @Autowired public TestController(TagService tagService) { this.tagService = tagService; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。