1. 程式人生 > >spring mvc常用註解@Component @Controller @Service @Repository

spring mvc常用註解@Component @Controller @Service @Repository

註解用了之後,會在*.xml檔案中大大減少配置量。以前我們每個Bean都得到配置檔案中配置關聯下。spring2.5後,引入了完整的annotation配置註解,使得我們的程式配置更簡單更容易維護。 

@Component;@Controller;@Service;@Repository 

      在annotaion配置註解中用@Component來表示一個通用註釋用於說明一個類是一個spring容器管理的類。即就是該類已經拉入到spring的管理中了。而@Controller, @Service, @Repository是@Component的細化,這三個註解比@Component帶有更多的語義,它們分別對應了控制層、服務層、持久層的類。 


@Repository標籤是用來給持久層的類定義一個名字,讓Spring根據這個名字關聯到這個類。 


例如: 

@Repository("userDao") 
public class UserDaoImpl  implements UserDao{ 

   ........................................ 



聲明瞭UserDaoImpl  在Spring容器中叫userDao這個名字。 

@Service是用於服務層的IServiceImpl類檔案,功能與@Repository類似。 



另外標籤:@Autowired 用來注入。 

例如: 

@Autowired 
private UserDao userDao; 


這樣就注入進去了,相當於我們new了個實現類,我們就無需寫setter方法了。 

我們還得有配置檔案進行配置: 

Xml程式碼  收藏程式碼
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.        xmlns:aop="http://www.springframework.org/schema/aop"   
  5.        xmlns:tx
    ="http://www.springframework.org/schema/tx"   
  6.        xmlns:context="http://www.springframework.org/schema/context"   
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  8.      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
  9.      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  10.      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">   
  11.     <context:annotation-config/>   
  12.     <context:component-scan base-package="com.zxr.manager">   
  13.         <context:include-filter type="regex" expression=".*DaoImpl"/>   
  14.         <context:include-filter type="regex" expression=".*ServiceImpl"/>   
  15.     </context:component-scan>   
  16. </beans>   


這樣就把com.zxr.manager包下的所有.*DaoImpl,.*ServiceImpl都註冊關聯到Spring容器中去了。 

------------------------------------------------------------- 
@Service用於標註業務層元件 

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

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

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

把DAO實現類注入到action的service介面(注意不要是service的實現類)中,注入時不要new 這個注入的類,因為spring會自動注入,如果手動再new的話會出現錯誤, 
然後屬性加上@Autowired後不需要getter()和setter()方法,Spring也會自動注入。  

在介面前面標上@Autowired註釋使得介面可以被容器注入,如: 

[java] view plaincopy 
@Autowired  
@Qualifier("chinese")  
private Man man;   

當介面存在兩個實現類的時候必須使用@Qualifier指定注入哪個實現類,否則可以省略,只寫@Autowired。

----------------------------------------------------------------------------------------------------------------------------------------

spring 自 2.0 版本開始,陸續引入了一些註解用於簡化 Spring 的開發。@Repository註解便屬於最先引入的一批,它用於將資料訪問層 (DAO 層 ) 的類標識為 Spring Bean。具體只需將該註解標註在 DAO類上即可。同時,為了讓 Spring 能夠掃描類路徑中的類並識別出 @Repository 註解,需要在 XML 配置檔案中啟用Bean 的自動掃描功能,這可以通過<context:component-scan/>實現。如下所示:

 // 首先使用 @Repository 將 DAO 類宣告為 Bean 
 package bookstore.dao; 
 @Repository 
 public class UserDaoImpl implements UserDao{ …… } 

 // 其次,在 XML 配置檔案中啟動 Spring 的自動掃描功能
 <beans … > 
    ……
 <context:component-scan base-package=”bookstore.dao” /> 
……
 </beans> 

如此,我們就不再需要在 XML 中顯式使用 <bean/> 進行Bean 的配置。Spring 在容器初始化時將自動掃描 base-package 指定的包及其子包下的所有 class檔案,所有標註了 @Repository 的類都將被註冊為 Spring Bean。

為什麼 @Repository 只能標註在 DAO 類上呢?這是因為該註解的作用不只是將類識別為Bean,同時它還能將所標註的類中丟擲的資料訪問異常封裝為 Spring 的資料訪問異常型別。 Spring本身提供了一個豐富的並且是與具體的資料訪問技術無關的資料訪問異常結構,用於封裝不同的持久層框架丟擲的異常,使得異常獨立於底層的框架。

Spring 2.5 在 @Repository的基礎上增加了功能類似的額外三個註解:@Component、@Service、@Constroller,它們分別用於軟體系統的不同層次:

  • @Component 是一個泛化的概念,僅僅表示一個元件 (Bean) ,可以作用在任何層次。
  • @Service 通常作用在業務層,但是目前該功能與 @Component 相同。
  • @Constroller 通常作用在控制層,但是目前該功能與 @Component 相同。

通過在類上使用 @Repository、@Component、@Service 和 @Constroller 註解,Spring會自動建立相應的 BeanDefinition 物件,並註冊到 ApplicationContext 中。這些類就成了 Spring受管元件。這三個註解除了作用於不同軟體層次的類,其使用方式與 @Repository 是完全相同的。

另外,除了上面的四個註解外,使用者可以建立自定義的註解,然後在註解上標註 @Component,那麼,該自定義註解便具有了與所@Component 相同的功能。不過這個功能並不常用。

當一個 Bean 被自動檢測到時,會根據那個掃描器的 BeanNameGenerator 策略生成它的 bean名稱。預設情況下,對於包含 name 屬性的 @Component、@Repository、 @Service 和@Controller,會把 name 取值作為 Bean 的名字。如果這個註解不包含 name值或是其他被自定義過濾器發現的元件,預設 Bean 名稱會是小寫開頭的非限定類名。如果你不想使用預設 bean命名策略,可以提供一個自定義的命名策略。首先實現 BeanNameGenerator介面,確認包含了一個預設的無引數構造方法。然後在配置掃描器時提供一個全限定類名,如下所示:

 <beans ...> 
 <context:component-scan 
    base-package="a.b" name-generator="a.SimpleNameGenerator"/> 
 </beans> 

與通過 XML 配置的 Spring Bean 一樣,通過上述註解標識的Bean,其預設作用域是"singleton",為了配合這四個註解,在標註 Bean 的同時能夠指定 Bean 的作用域,Spring2.5 引入了 @Scope 註解。使用該註解時只需提供作用域的名稱就行了,如下所示:

 @Scope("prototype") 
 @Repository 
 public class Demo { … } 

如果你想提供一個自定義的作用域解析策略而不使用基於註解的方法,只需實現 ScopeMetadataResolver介面,確認包含一個預設的沒有引數的構造方法。然後在配置掃描器時提供全限定類名:

 <context:component-scan base-package="a.b"
 scope-resolver="footmark.SimpleScopeResolver" />