@Autowired is an annotation that can mark class member variables, methods and constructors, allowing spring to complete the work of bean autowiring.
@Autowired defaults to matching by class, and @Qualifier specifies to assemble beans by name.
Common usage:
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; } }
The above is the detailed content of What does @Autowired do?. For more information, please follow other related articles on the PHP Chinese website!