转自:
@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 方法@Autowiredprivate TypeService typeService;//set 方法使用 @Autowiredprivate ArticleService articleService;@Autowiredpublic void setArticleService(ArticleService articleService) {this.articleService = articleService;}//构造方法使用 @Autowiredprivate TagService tagService;@Autowiredpublic TestController(TagService tagService) {this.tagService = tagService; } }