zyskm用夢想丈量人生,用奔跑丈量激情

          spring3系列 二、Spring配置項解釋說明

          這篇也是轉載,改了中間部分內。

          在基于注解方式配置
          Spring的配置文件中,你可能會見到<context:annotation-config/>這樣一條配置,他的作用是式地向 Spring 容器注冊

          AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、

          PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor  4 BeanPostProcessor。

          注冊這4 BeanPostProcessor的作用,就是為了你的系統能夠識別相應的注解。

          例如:

          如果你想使用@Autowired注解,那么就必須事先在 Spring 容器中聲明 AutowiredAnnotationBeanPostProcessor Bean。傳統聲明方式如下:

          1. <bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/> 

          如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必須聲明CommonAnnotationBeanPostProcessor

          如果想使用@PersistenceContext注解,就必須聲明PersistenceAnnotationBeanPostProcessorBean。

          如果想使用 @Required的注解,就必須聲明RequiredAnnotationBeanPostProcessorBean。同樣,傳統的聲明方式如下:

          1. <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> 

          一般來說,這些注解我們還是比較常用,尤其是Antowired的注解,在自動注入的時候更是經常使用,所以如果總是需要按照傳統的方式一條一條配置顯得有些繁瑣和沒有必要,于是spring給我們提供<context:annotation-config/>的簡化配置方式,自動幫你完成聲明。

             不過,呵呵,我們使用注解一般都會配置掃描包路徑選項

          1. <context:component-scan base-package=”XX.XX”/> 

              該配置項其實也包含了自動注入上述processor的功能,因此當使用 <context:component-scan/> 后,就可以將 <context:annotation-config/> 移除了。

          本文轉載:http://mushiqianmeng.blog.51cto.com/3970029/723880

          posted on 2012-04-13 15:14 zyskm 閱讀(2575) 評論(4)  編輯  收藏

          評論

          # re: spring3系列 二、Spring配置項解釋說明 2012-04-13 15:50 周轉箱模具

          博客越來越少了……博主你一定要頂住?。∥抑С帜?!  回復  更多評論   

          # re: spring3系列 二、Spring配置項解釋說明 2012-04-25 17:00 路人甲

          樓主,你知道spring怎么配置或者怎么實現,在使用serviceBean的時候,只需要在action里建一個serviceBean的屬性,就可以直接使用,而不用通過spring的bean工廠去getBean(“xxx”),如下代碼

          。。。

          private UserInfoManagement userInfoManagement;

          public String loginSys(){
          UUserInfo user = userInfoManagement.getUserByName(username);

          。。。  回復  更多評論   

          # re: spring3系列 二、Spring配置項解釋說明 2012-04-26 16:10 zyskm

          自動裝配一共兩種方式實現:
          1.在xml顯示配置;
          2.通過注解方式實現,如在serviceBean增加@Service("xxxServiceBean"),在action中就可以實現自動裝配。@Component("xxxServiceBean")也可以,@service更準確些。@路人甲
            回復  更多評論   

          # re: spring3系列 二、Spring配置項解釋說明 2012-05-23 13:29 瘦腿精油

          Spring很強大  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 积石山| 兴山县| 明溪县| 沂源县| 桓台县| 石阡县| 昌江| 东兰县| 麻城市| 瑞安市| 新余市| 翼城县| 敦煌市| 蒙山县| 东丽区| 皋兰县| 吴江市| 察哈| 台州市| 白银市| 龙泉市| 泸西县| 东乌珠穆沁旗| 盘山县| 宁陵县| 从江县| 临夏市| 聂荣县| 克拉玛依市| 丹江口市| 珠海市| 永胜县| 嵩明县| 安宁市| 两当县| 获嘉县| 襄城县| 唐山市| 福海县| 双峰县| 江源县|