<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    Spring 2.5 中除了提供 @Component 注釋外,還定義了幾個擁有特殊語義的注釋,它們分別是:@Repository、@Service 和 @Controller。在目前的 Spring 版本中,這 3 個注釋和 @Component 是等效的,但是從注釋類的命名上,很容易看出這 3 個注釋分別和持久層、業務層和控制層(Web 層)相對應。雖然目前這 3 個注釋和 @Component 相比沒有什么新意,但 Spring 將在以后的版本中為它們添加特殊的功能。所以,如果 Web 應用程序采用了經典的三層分層結構的話,最好在持久層、業務層和控制層分別采用 @Repository、@Service 和 @Controller 對分層中的類進行注釋,而用 @Component 對那些比較中立的類進行注釋。

    在 一個稍大的項目中,通常會有上百個組件,如果這些組件采用xml的bean定義來配置,顯然會增加配置文件的體積,查找以及維護起來也不太方便。 Spring2.5為我們引入了組件自動掃描機制,他可以在類路徑底下尋找標注了 @Component,@Service,@Controller,@Repository注解的類,并把這些類納入進spring容器中管理。它的作用 和在xml文件中使用bean節點配置組件時一樣的。要使用自動掃描機制,我們需要打開以下配置信息: 
    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用于標注業務層組件,

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

         @Repository用于標注數據訪問組件,即DAO組件,

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

        */   


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

    /*getBean的默認名稱是類名(頭字母小 寫),如果想自定義,可以@Service(“aaaaa”)這樣來指定,這種bean默認是單例的,如果想改變,可以使用 @Service(“beanName”) @Scope(“prototype”)來改變。可以使用以下方式指定初始化方法和銷毀方法(方法名任意): @PostConstruct public void init() {  

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

    注入方式:

    把 DAO實現類注入到service實現類中,把service的接口(注意不要是service的實現類)注入到action中,注入時不要new 這個注入的類,因為spring會自動注入,如果手動再new的話會出現錯誤,然后屬性加上@Autowired后不需要getter()和 setter()方法,Spring也會自動注入。至于更具體的內容,等對注入的方式更加熟練后會做個完整的例子上來。

    注解:

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

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

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

    在接口前面標上@Autowired和@Qualifier注釋使得接口可以被容器注入,當接口存在兩個實現類的時候必須指定其中一個來注入,使用實現類首字母小寫的字符串來注入,如:

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

    否則可以省略,只寫@Autowired   。 

    @Service服務層組件,用于標注業務層組件,表示定義一個bean,自動根據bean的類名實例化一個首寫字母為小寫的bean,例如Chinese實例化為chinese,如果需要自己改名字則:@Service("你自己改的bean名")。   

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

    @Repository持久層組件,用于標注數據訪問組件,即DAO組件

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


    @Service 
    public class VentorServiceImpl implements iVentorService { 
    }

    @Repository 
    public class VentorDaoImpl implements iVentorDao { 


    getBean 的默認名稱是類名(頭字母小寫),如果想自定義,可以@Service(“aaaaa”) 這樣來指定,這種

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

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

    @PostConstruct

    public void init() { 



    @PreDestroy

    public void destory() { 

    }

    posted on 2011-10-10 16:46 AK47 閱讀(49716) 評論(3)  編輯  收藏 所屬分類: Spring

    評論

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

    spring代碼下載 :http://www.zuidaima.com/share/search.htm?key=spring  回復  更多評論   

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

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

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

    5454554  回復  更多評論   

    <2013年9月>
    25262728293031
    1234567
    891011121314
    15161718192021
    22232425262728
    293012345

    導航

    統計

    常用鏈接

    留言簿

    隨筆分類

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 我想看一级毛片免费的| 日本黄网站动漫视频免费| 免费一看一级毛片全播放| 亚洲色偷偷综合亚洲AV伊人蜜桃| 91精品国产免费| 亚洲综合激情九月婷婷| 亚洲免费视频观看| 亚洲av日韩av综合| 成人免费毛片观看| 久久精品国产亚洲av品善| 国产一级淫片免费播放电影| 国产成人亚洲精品91专区高清| 国产一区二区三区在线免费观看| 青草久久精品亚洲综合专区| 亚洲成a人一区二区三区| 一个人看的免费观看日本视频www 一个人看的免费视频www在线高清动漫 | 久久综合日韩亚洲精品色| 国产午夜无码精品免费看| 亚洲五月激情综合图片区| 99热在线精品免费播放6| 亚洲一区二区三区免费视频| 毛片a级三毛片免费播放| 成人婷婷网色偷偷亚洲男人的天堂| 亚洲AV无码乱码在线观看牲色| 久久国产精品免费| 亚洲精品白色在线发布| 卡一卡二卡三在线入口免费| 一级成人毛片免费观看| 亚洲AV美女一区二区三区| 青青草免费在线视频| 特级毛片爽www免费版| 亚洲成人免费在线| 国内自产拍自a免费毛片| 深夜A级毛片视频免费| 亚洲av日韩av不卡在线观看| 性色av无码免费一区二区三区| 亚欧洲精品在线视频免费观看 | 国产卡二卡三卡四卡免费网址| 菠萝菠萝蜜在线免费视频| 亚洲av日韩av高潮潮喷无码| 日韩一区二区免费视频|