首頁  >  文章  >  Java  >  @Autowired的作用是什麼?

@Autowired的作用是什麼?

(*-*)浩
(*-*)浩轉載
2019-09-09 16:55:459764瀏覽

@Autowired 是個註釋,它可以對類別成員變數、方法及建構子進行標註,讓 spring 完成 bean 自動組裝的工作。

@Autowired的作用是什麼?

@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; 
	}
	
}

以上是@Autowired的作用是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除