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

 


原文鏈接
 


 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM