(轉(zhuǎn)載)Spring 注解@Component,@Service,@Controller,@Repository

          Spring 2.5 中除了提供 @Component 注釋外,還定義了幾個(gè)擁有特殊語(yǔ)義的注釋?zhuān)鼈兎謩e是:@Repository、@Service 和 @Controller。在目前的 Spring 版本中,這 3 個(gè)注釋和 @Component 是等效的,但是從注釋類(lèi)的命名上,很容易看出這 3 個(gè)注釋分別和持久層、業(yè)務(wù)層和控制層(Web 層)相對(duì)應(yīng)。雖然目前這 3 個(gè)注釋和 @Component 相比沒(méi)有什么新意,但 Spring 將在以后的版本中為它們添加特殊的功能。所以,如果 Web 應(yīng)用程序采用了經(jīng)典的三層分層結(jié)構(gòu)的話(huà),最好在持久層、業(yè)務(wù)層和控制層分別采用 @Repository、@Service 和 @Controller 對(duì)分層中的類(lèi)進(jìn)行注釋?zhuān)?@Component 對(duì)那些比較中立的類(lèi)進(jìn)行注釋。

          在 一個(gè)稍大的項(xiàng)目中,通常會(huì)有上百個(gè)組件,如果這些組件采用xml的bean定義來(lái)配置,顯然會(huì)增加配置文件的體積,查找以及維護(hù)起來(lái)也不太方便。 Spring2.5為我們引入了組件自動(dòng)掃描機(jī)制,他可以在類(lèi)路徑底下尋找標(biāo)注了 @Component,@Service,@Controller,@Repository注解的類(lèi),并把這些類(lèi)納入進(jìn)spring容器中管理。它的作用 和在xml文件中使用bean節(jié)點(diǎn)配置組件時(shí)一樣的。要使用自動(dòng)掃描機(jī)制,我們需要打開(kāi)以下配置信息: 
          Java代碼

          1. <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-2.5.xsd"  
          2. >  
          3.   
          4. <context:component-scan base-package=”com.eric.spring”>   
          5. </beans>   
             /*其中base-package為需要掃描的包(含所有子包)

               @Service用于標(biāo)注業(yè)務(wù)層組件,

               @Controller用于標(biāo)注控制層組件(如struts中的action),

               @Repository用于標(biāo)注數(shù)據(jù)訪(fǎng)問(wèn)組件,即DAO組件,

               @Component泛指組件,當(dāng)組件不好歸類(lèi)的時(shí)候,我們可以使用這個(gè)注解進(jìn)行標(biāo)注。

              */   


          6. @Service public class VentorServiceImpl implements iVentorService {   
          7. } @Repository public class VentorDaoImpl implements iVentorDao {  
          8. }

          /*getBean的默認(rèn)名稱(chēng)是類(lèi)名(頭字母小 寫(xiě)),如果想自定義,可以@Service(“aaaaa”)這樣來(lái)指定,這種bean默認(rèn)是單例的,如果想改變,可以使用 @Service(“beanName”) @Scope(“prototype”)來(lái)改變??梢允褂靡韵路绞街付ǔ跏蓟椒ê弯N(xiāo)毀方法(方法名任意): @PostConstruct public void init() {  

          */
          9. }  
          10. @PreDestroy public void destory() {  
          11. } 

          注入方式:

          把 DAO實(shí)現(xiàn)類(lèi)注入到service實(shí)現(xiàn)類(lèi)中,把service的接口(注意不要是service的實(shí)現(xiàn)類(lèi))注入到action中,注入時(shí)不要new 這個(gè)注入的類(lèi),因?yàn)閟pring會(huì)自動(dòng)注入,如果手動(dòng)再new的話(huà)會(huì)出現(xiàn)錯(cuò)誤,然后屬性加上@Autowired后不需要getter()和 setter()方法,Spring也會(huì)自動(dòng)注入。至于更具體的內(nèi)容,等對(duì)注入的方式更加熟練后會(huì)做個(gè)完整的例子上來(lái)。

          注解:

          在 spring的配置文件里面只需要加上<context:annotation-config/> 和<context:component-scan base-package="需要實(shí)現(xiàn)注入的類(lèi)所在包"/>,可以使用base-package="*"表示全部的類(lèi)。   

          <context:component-scan base-package=”com.eric.spring”> 

          其中base-package為需要掃描的包(含所有子包)

          在接口前面標(biāo)上@Autowired和@Qualifier注釋使得接口可以被容器注入,當(dāng)接口存在兩個(gè)實(shí)現(xiàn)類(lèi)的時(shí)候必須指定其中一個(gè)來(lái)注入,使用實(shí)現(xiàn)類(lèi)首字母小寫(xiě)的字符串來(lái)注入,如:

          1.     @Autowired     
          2.   
          3.     @Qualifier("chinese")      
          4.   
          5.     private Man man;   

          否則可以省略,只寫(xiě)@Autowired   。 

          @Service服務(wù)層組件,用于標(biāo)注業(yè)務(wù)層組件,表示定義一個(gè)bean,自動(dòng)根據(jù)bean的類(lèi)名實(shí)例化一個(gè)首寫(xiě)字母為小寫(xiě)的bean,例如Chinese實(shí)例化為chinese,如果需要自己改名字則:@Service("你自己改的bean名")。   

          @Controller用于標(biāo)注控制層組件(如struts中的action)

          @Repository持久層組件,用于標(biāo)注數(shù)據(jù)訪(fǎng)問(wèn)組件,即DAO組件

          @Component泛指組件,當(dāng)組件不好歸類(lèi)的時(shí)候,我們可以使用這個(gè)注解進(jìn)行標(biāo)注。 


          @Service 
          public class VentorServiceImpl implements iVentorService { 
          }

          @Repository 
          public class VentorDaoImpl implements iVentorDao { 


          getBean 的默認(rèn)名稱(chēng)是類(lèi)名(頭字母小寫(xiě)),如果想自定義,可以@Service(“aaaaa”) 這樣來(lái)指定,這種

          bean默認(rèn)是單例的,如果想改變,可以使用@Service(“beanName”) @Scope(“prototype”)來(lái)改變。

          可以使用以下方式指定初始化方法和銷(xiāo)毀方法(方法名任意):

          @PostConstruct

          public void init() { 



          @PreDestroy

          public void destory() { 

          }

          posted on 2011-10-10 16:46 AK47 閱讀(49719) 評(píng)論(3)  編輯  收藏 所屬分類(lèi): Spring

          評(píng)論

          # re: (轉(zhuǎn)載)Spring 注解@Component,@Service,@Controller,@Repository 2014-06-18 22:51 zuidaima

          spring代碼下載 :http://www.zuidaima.com/share/search.htm?key=spring  回復(fù)  更多評(píng)論   

          # re: (轉(zhuǎn)載)Spring 注解@Component,@Service,@Controller,@Repository 2014-09-14 10:04 tttt

          <script type="text/javascript">
          alert(''aaaa')
          </script>  回復(fù)  更多評(píng)論   

          # re: (轉(zhuǎn)載)Spring 注解@Component,@Service,@Controller,@Repository 2015-12-22 14:20 4554

          5454554  回復(fù)  更多評(píng)論   


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2014年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆分類(lèi)

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 望奎县| 含山县| 金塔县| 安化县| 奉新县| 宽甸| 新野县| 铁岭市| 凤庆县| 武清区| 闵行区| 石渠县| 贵南县| 泰宁县| 宁都县| 万年县| 东阳市| 禹城市| 清丰县| 岳西县| 武夷山市| 余姚市| 康保县| 石屏县| 治县。| 阿拉善左旗| 河源市| 大连市| 昌都县| 青田县| 仲巴县| 望江县| 黑水县| 盘山县| 绥宁县| 霍城县| 沈阳市| 五常市| 蒙城县| 龙口市| 崇州市|