@Autowired具有什么功能呢?
下文笔者讲述@Autowired注解的功能简介说明,如下所示
@Autowired注解简介
@Autowired 是一个注解
它可以对类成员变量、方法及构造函数进行标注
让spring完成bean自动装配工作
@Autowired默认是按照类去匹配
配合@Qualifier指定按照名称去装配bean
例:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.java265.service.ArticleService;
import com.java265.service.TagService;
import com.java265.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;
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


