【Spring】10、Spring中用@Component、@Repository、@Service和 @Controller等標註的預設Bean名稱會是小寫開頭的非限定類名,repositorycomponent

來源:互聯網
上載者:User

【Spring】10、Spring中用@Component、@Repository、@Service和 @Controller等標註的預設Bean名稱會是小寫開頭的非限定類名,repositorycomponent

@Service用於標註業務層組件(我們通常定義的service層就用這個)

@Controller用於標註控制層組件(如struts中的action)

@Repository用於標註資料訪問組件,即DAO組件

@Component泛指組件,當組件不好歸類的時候,我們可以使用這個註解進行標註。

 

使用過濾器自訂掃描 <context:component-scan base-package="org.example"> 
spring開發手冊中這樣寫: 
3.12.4. 自動檢測組件的命名

當一個組件在某個掃描過程中被自動檢測到時,會根據那個掃描器的BeanNameGenerator 策略產生它的bean名稱。預設情況下,任何包含 name值的Spring“典型”註解 (@Component、@Repository、 @Service和@Controller) 會把那個名字 提供給相關的bean定義。如果這個註解不包含name值或是其他檢測到的組件 (比如被自訂過濾器發現的),預設bean名稱產生器會返回小寫開頭的非限定(non-qualified)類名。 例如,如果發現了下面這兩個組件,它們的名字會是‘myMovieLister‘和‘movieFinderImpl‘:

@Service("myMovieLister")public class SimpleMovieLister {    // ...}@Repositorypublic class MovieFinderImpl implements MovieFinder {    // ...}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.