@Autowired 是一个注释,它可以对类成员变量、方法及构造函数进行标注,让 spring 完成 bean 自动装配的工作。@Autowired 默认是按照类去匹配,配合 @Qualifier 指定按照名称去装配 bean。常见用法:i
@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;
}
}
--结束END--
本文标题: @Autowired的作用是什么?
本文链接: https://www.lsjlt.com/news/2446.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-03
2024-04-03
2024-04-01
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0