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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

@Controller,@Service,@Repository,@Component详解

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

轉載自?@Controller,@Service,@Repository,@Component詳解

@Controller


用來表示一個web控制層bean,如SpringMvc中的控制器。


@Service


用來表示一個業務層bean。


@Repository


用來表示一個持久層bean,即數據訪問層DAO組件。


@Component


用來表示一個平常的普通組件,當一個類不合適用以上的注解定義時用這個組件修飾。




需要注意的是@Controller,@Service,@Repository都有帶@Component父注解,說明它們除了基本組件的屬性外還有其他的的場景應用,即如果不用SpringMVC其實它們就是一個普通的組件,但普通組件建議最好還是用@Component修飾。



為了讓Spring自動掃描注冊這些組件,需要在配置文件中加上掃描的配置,如掃描com.test包下的注解。


<context:component-scan base-package="com.test" />


些掃描配置默認use-default-filters="true",默認掃描@Component注解及子注解,可以配置過濾只掃描哪些注解不掃描哪些注解。


要過濾掃描注解,需要相應的帶上下面的子標簽,可以有多個。


<context:include-filter>

<context:exclude-filter>


如只掃描com.test包下的@Controller和@Service注解的組件。


<context:component-scan base-package="com.test"?use-default-filters="false">

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>?

<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>?

</context:component-scan>


關于type的定義


Filter TypeExamples ExpressionDescription
annotationorg.example.SomeAnnotation符合SomeAnnoation的target class
assignableorg.example.SomeClass指定class或interface的全名
aspectjorg.example..*Service+AspectJ語法
regexorg\.example\.Default.*Regelar Expression
customorg.example.MyTypeFilter自定義Type,實現接口org.springframework.core.type.TypeFilter


另外,<context:component-scan>配置可以有多個。


總結

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

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