@Autowired的作用是什么?

内容摘要
@Autowired 默认是按照类去匹配,配合 @Qualifier 指定按照名称去装配 bean。常见用法:import org.springframework.beans.factory.annotation.Autowired;
import org.springfr
文章正文

@Autowired 默认是按照类去匹配,配合 @Qualifier 指定按照名称去装配 bean。

常见用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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;
    }
     
}

代码注释
[!--zhushi--]

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!