日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring中stereotype注解Component,Repository,Service,Controller

發布時間:2023/12/3 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring中stereotype注解Component,Repository,Service,Controller 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【README】

本文介紹了 spring4.0 下 org.springframework.stereotype 的注解類型,俗稱刻板型注解(一成不變型);

包括 @Component, @Repository,@Service, @Controller ;

目錄

【README】

【1】@Component注解(組件)

【2】@Repository注解(倉庫)

【3】@Service注解(服務)

【4】@Controller注解(控制器)



【1】@Component注解(組件)

因為 Service, Repository , Controller 都使用到了 Component注解;被 @Controller修飾的注解被視為 spring自動掃描的候選對象;

表示帶此注解的類是“組件”。 在使用基于注解的配置和類路徑掃描時,此類被視為自動檢測的候選對象
其他注解也可以被視為標識一個類為組件,典型的如 @Repository 或 AspectJ 的 @Aspect 注解。

@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Component {/*** 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 "";}


【2】@Repository注解(倉庫)

1.表示帶該注解的類是“存儲庫”或倉庫,最初由領域驅動設計 (Evans, 2003) 定義為“一種用于封裝存儲、檢索和搜索行為的機制,它模擬了一個對象集合”。

2.實現傳統 Java EE 模式(例如“數據訪問對象”)的團隊也可以將此注解應用于 DAO 類,但在執行此操作之前應注意理解數據訪問對象DAO和 DDD 樣式倉庫之間的區別

3.當與 PersistenceExceptionTranslationPostProcessor 結合使用時,該注解修飾的類可以進行 Spring DataAccessException 轉換。

4.從 Spring 2.5 開始,該注解也作為 @Component 的特例,允許通過類路徑掃描自動檢測被注解的類。

@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Repository {/*** 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 "";}

【3】@Service注解(服務)

表示帶該注解的類是“服務”,最初由領域驅動設計(Evans,2003)定義為“作為獨立于模型中的接口提供的操作,沒有封裝狀態”。
也可以表示被修飾的類是“業務服務外觀”。 此注解是通用的版型對象(一成不變),個別團隊可能會縮小其語義范圍并酌情使用。

該注解作為@Component 的特例,允許使用方通過類路徑掃描自動檢測被注解的類。

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

【4】@Controller注解(控制器)

1.表示帶該注釋的類是“控制器”(例如 Web 控制器)
2.該注解作為@Component 的特例,允許通過類路徑掃描自動檢測實現類。

3.它通常與 RequestMapping注解修飾的處理器方法結合使用。

org.springframework.web.bind.annotation.RequestMapping

@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Controller {/*** 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 "";}

總結

以上是生活随笔為你收集整理的spring中stereotype注解Component,Repository,Service,Controller的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。