聊聊注解@controller@service@component@repository的區(qū)別
注解@controller@service@component@repository的區(qū)別
查了一些網(wǎng)上的其他博客,發(fā)現(xiàn)幾個注解本質(zhì)上沒有什么區(qū)別,至少在spring2.5版本里,這幾個注解本質(zhì)是一樣的(當然,新的版本有什么變化目前還沒細查)
命名不一樣主要是為了區(qū)分類的作用和所屬層級:
@Repository:持久層,用于標注數(shù)據(jù)訪問組件,即DAO組件。@Service:業(yè)務層,用于標注業(yè)務邏輯層主鍵。@Controller:控制層,用于標注控制層組件。@Component:當你不確定是屬于哪一層的時候使用。
之所以區(qū)分開幾種類型,一是spring想在以后的版本中為它們添加特殊技能,二是這種分層的做法使web架構(gòu)更清晰,易讀性與維護性更好。
/**
* Indicates that an annotated class is a "Service", originally defined by Domain-Driven
* Design (Evans, 2003) as "an operation offered as an interface that stands alone in the
* model, with no encapsulated state."
*
* <p>May also indicate that a class is a "Business Service Facade" (in the Core J2EE
* patterns sense), or something similar. This annotation is a general-purpose stereotype
* and individual teams may narrow their semantics and use as appropriate.
*
* <p>This annotation serves as a specialization of {@link Component @Component},
* allowing for implementation classes to be autodetected through classpath scanning.
*
* @author Juergen Hoeller
* @since 2.5
* @see Component
* @see Repository
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any
*/
String value() default "";
}
從@service的源碼看,service仍然是使用了@Component注解(@Controller與@Repository與service一樣,這里就不貼源碼了)。
component即組件,相當于xml配置文件中的bean申明,通過spring的自動掃描機制,在規(guī)定的類路徑中尋找標注了@Component,@Service,@Repository,@Controller注解的類,并把這些類納入進容器中進行管理。
getBean的默認名稱是類名(頭字母小寫),并且是單例的,如果想自定義,可以使用@Service(“beanName”)@Scope(“prototype”)來改變。
/**自動掃描base-package目錄下的所有文件,包括子目錄**/ <context:component-scan base-package="com.user.*"/>
Spring中的主要注解
Spring的一個核心功能是IOC,就是將Bean初始化加載到容器中, Bean加載到容器中可以使用 Spring注解方式或者 Spring XML配置方式。(通過注解將java中的一些類,加載到容器中)
1. 組件類注解@Component、@Repository、@Service、@Controller【創(chuàng)建注解】
@Repository、@Service、@Controller的功能和@Component相同,但為了使標注類的用途更加清晰(層次化),在實際開發(fā)中推薦使用:
@Repository標注數(shù)據(jù)訪問層(DAO層)、使用@Service標注業(yè)務邏輯層(Service層)以及使用@Controller標注控制器層(控制層)。
1.@Component 標注為一個普通的spring Bean類
該注解是一個泛化的概念,僅僅表示一個組件對象(Bean),可以作用在任何層次上。
2.@Repository 標注為一個DAO層的組件類
該注解用于將數(shù)據(jù)訪問層(DAO)的類標識為Bean,即注解數(shù)據(jù)訪問層Bean,其功能與@Component()相同。
3.@Service 標注為Service層(業(yè)務邏輯層)的組件類
該注解用于標注一個業(yè)務邏輯組件類(Service層),其功能與@Component()相同。
4.@Controller 標注一個控制器組件類
該注解用于標注一個控制器組件類(Spring MVC的Controller),其功能與@Component()相同。
2. 裝配bean時常用注解 @Autowired、@Resource【獲取注解】
2.1 @Autowired【***常用】
該注解可以對類成員變量、方法及構(gòu)造方法進行標注,完成自動裝配的工作(按照Bean的類型進行裝配)。 通過 @Autowired的使用來消除setter 和getter方法。默認按照Bean的類型進行裝配。(屬于Spring 的org.springframework.beans.factory.annotation包)
通過@Autowired注解,如果在容器中有對應的bean,就可以通過@Autowired自動裝載,也就是賦值。裝載之后自動的按照類型在spring容器中查找相同類型,然后為該字段其注入那個類型的bean實例。
其實就是一種依賴注入的方式,此方式必須確保加注解的類在spring中有對應的bean(怎樣加進去不管),并且字段的類型需要在spring容器中有相同類型的bean,才能創(chuàng)建bean實例,為其注入。
@Autowired注解就相當于從Spring容器中通過類型,實例化了當前對象,可以直接調(diào)用它的方法。
@Autowired private UserService userService; // 相當于執(zhí)行了實例化 private UserService userService = new UserServiceImpl();
2.2 @Resource(不屬于spring的注解,是javax.annotation注解)
該注解與@Autowired功能一樣。區(qū)別在于,該注解默認是按照名稱來裝配注入的,只有當找不到與名稱匹配的Bean才會按照類型來裝配注入;而@Autowired默認按照Bean的類型進行裝配,如果想按照名稱來裝配注入,則需要結(jié)合@Qualifier注解一起使用。
@Resource注解有兩個屬性:name和type。name屬性指定Bean實例名稱,即按照名稱來裝配注入;type屬性指定Bean類型,即按照Bean的類型進行裝配。
2.3 @Qualifier(不能單獨使用)
該注解與@Autowired注解配合使用。當@Autowired注解需要按照名稱來裝配注入,則需要結(jié)合該注解一起使用,Bean的實例名稱由@Qualifier注解的參數(shù)指定。
2.4 @Autowired和@Qualifier的混合使用
@Autowired注解默認按照類型裝配,如果容器中包含多個同一類型的Bean,那么啟動容器時會報找不到指定類型bean的異常,解決辦法是結(jié)合 @Qualifier 注解進行限定,指定注入的bean名稱。
比如接口BaseInterface,它有兩個實現(xiàn)類AClass和BClass,如果用@Autowired自動裝載BaseInterface,會無法識別到底是哪一個實現(xiàn)類(AClass/BClass),所以就需要用@Qualifier來配合 @Autowired區(qū)分。
public class Demo{
@Autowired
@Qualifier("id值") //默認類名首字母小寫 @Qualifier("aClass")
private BaseInterface base;
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot?整合?Spring-Session?實現(xiàn)分布式會話項目實戰(zhàn)
本文主要介紹了SpringBoot?整合?Spring-Session?實現(xiàn)分布式會話項目實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07
JAVA實現(xiàn)漢字轉(zhuǎn)拼音功能代碼實例
這篇文章主要介紹了JAVA實現(xiàn)漢字轉(zhuǎn)拼音功能代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05
FasfDFS整合Java實現(xiàn)文件上傳下載功能實例詳解
這篇文章主要介紹了FasfDFS整合Java實現(xiàn)文件上傳下載功能實例詳解,需要的朋友可以參考下2017-08-08
nodejs連接dubbo服務的java工程實現(xiàn)示例
這篇文章主要介紹了在項目遷移中,nodejs連接dubbo服務的java工程實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-03-03

