@Autowired 的作用是什么?

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/meism5/article/details/90446789

@Autowired 的作用是什么?

@Autowired 是一个注释,它可以对类成员变量、方法及构造函数进行标注,让 spring 完成 bean 自动装配的工作。
@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; 
	}
	
}

收藏、分享最有参考价值的编程信息 

博客 [ ConstXiong ]

公众号 [ ConstXiong ]

猜你喜欢

转载自blog.csdn.net/meism5/article/details/90446789