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

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

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

    隨筆-314  評論-209  文章-0  trackbacks-0
    轉自:http://www.iteye.com/topic/255397

    1.那即將離我遠去的

    用buffalo作為我的ajax類庫也有些歷史了,幾乎是和Spring同時開始接觸的.

    按照官方的方式,Buffalo與Spring的集成是很簡單:

        在Spring中配置一個BuffaloServiceConfigure bean,把spring托管的服務在其中聲明即可,Buffalo可以通過ServletContext得到Spring的WebApplicationContext,進而得到所需的服務:

    Java代碼 復制代碼 收藏代碼
    1. <bean name="buffaloConfigBean"    
    2.                 class="net.buffalo.service.BuffaloServiceConfigurer">    
    3.                 <property name="services">    
    4.                         <map>    
    5.                                 <entry key="springSimpleService">    
    6.                                         <ref bean="systemService" />    
    7.                                 </entry>    
    8.                                 <entry key="springSimpleService2">    
    9.                                         <ref bean="systemService2" />    
    10.                                 </entry>    
    11.                         </map>    
    12.                 </property>    
    13.  </bean>   

     

         似乎很簡單,但,有沒有覺得似乎很傻?只是把Spring里已經配置好的bean再引用一次而已,

    一旦面臨協作開發,和所有的全局配置文件一樣,BuffaloServiceConfigure bean下面就會囊括幾十上百個service ref,一大堆人圍著這個配置文件轉,CVS沖突就成了家常便飯了,苦惱不已.當然,按我們這么多年的開發經驗是不會出現這種低級錯誤的,早早的在項目設計階段就會按模塊劃分出多個配置文件,一人獨用,無需和別人共享配置,輕松面對沖突問題,帶來的局面就是每個包里都塞著一個buffalo.xml,一個項目里配置文件到處有,不斷得copy/paste,層層套套,那可不是碩果累累的滿足感.

         當然,Spring本身在2.5之前也因XML配置繁瑣而讓人詬病,Guice才能異軍突起,那時Spring比Buffalo的配置更多,所以Buffalo的問題也就不是問題了.但有一天,我終于要正式升級到Spring2.5.

         世界清靜了!使用annotation,看到怎么多配置文件消失,看到簡潔的Bean/MVC配置,呵呵,還真是令人心情愉悅的.

         誒,等等,怎么還有大堆XML?哦?原來是Buffalo...

         Buffalo像個刺頭,傻愣愣地杵在XML里.

     

    2.于是我開始把Buffalo也Annotation化.

     

    話說Spring的擴展能力還是ganggang的,一天時間,就有成果了.

    先寫個注解:

    Java代碼 復制代碼 收藏代碼
    1. package cn.tohot.common.annotation;   
    2.   
    3. import java.lang.annotation.ElementType;   
    4. import java.lang.annotation.Retention;   
    5. import java.lang.annotation.RetentionPolicy;   
    6. import java.lang.annotation.Target;   
    7.   
    8. /**  
    9.  * buffalo擴展接口,用于表明該類是一個buffalo服務.  
    10.  * @author tedeyang  
    11.  *  
    12.  */  
    13. @Retention(RetentionPolicy.RUNTIME)   
    14. @Target(ElementType.TYPE)   
    15. public @interface Buffalo {   
    16.     /**  
    17.      * @return 遠程調用時的服務名.  
    18.      */    
    19.     String value();   
    20. }  

     

    接著再寫Spring的擴展

     

    Java代碼 復制代碼 收藏代碼
    1. /**  
    2.  *   
    3.  */  
    4. package cn.tohot.common.annotation;   
    5.   
    6. import java.util.HashMap;   
    7.   
    8. import net.buffalo.service.BuffaloServiceConfigurer;   
    9.   
    10. import org.apache.log4j.Logger;   
    11. import org.springframework.beans.BeansException;   
    12. import org.springframework.beans.factory.DisposableBean;   
    13. import org.springframework.beans.factory.FactoryBean;   
    14. import org.springframework.beans.factory.InitializingBean;   
    15. import org.springframework.beans.factory.config.BeanPostProcessor;   
    16.   
    17. /**  
    18.  * 該類作為FactoryBean可以無縫替換buffalo 2.0自帶的配置類,并使用annotation進行配置.  
    19.  * @author tedeyang  
    20.  *  
    21.  */  
    22. @SuppressWarnings("unchecked")   
    23. public class BuffaloAnnotationServiceFactoryBean implements FactoryBean, InitializingBean, DisposableBean, BeanPostProcessor {   
    24.     private static final Logger log = Logger.getLogger(BuffaloAnnotationServiceFactoryBean.class);   
    25.   
    26.     private BuffaloServiceConfigurer buffaloConfigurer = null;   
    27.   
    28.     public BuffaloAnnotationServiceFactoryBean() {   
    29.         buffaloConfigurer = new BuffaloServiceConfigurer();   
    30.         buffaloConfigurer.setServices(new HashMap());   
    31.     }   
    32.   
    33.     private void addBuffaloBean(String buffaloServiceName,Object bean) {   
    34.         buffaloConfigurer.getServices().put(buffaloServiceName, bean);   
    35.         log.info("Add a buffalo service :"+buffaloServiceName);   
    36.     }   
    37.   
    38.     public Object getObject() throws Exception {   
    39.         return this.buffaloConfigurer;   
    40.     }   
    41.   
    42.     public Class getObjectType() {   
    43.         return BuffaloServiceConfigurer.class;   
    44.     }   
    45.   
    46.     public boolean isSingleton() {   
    47.         return true;   
    48.     }   
    49.   
    50.     public void afterPropertiesSet() throws Exception {   
    51.     }   
    52.   
    53.     public void destroy() throws Exception {   
    54.         if (buffaloConfigurer != null)   
    55.             buffaloConfigurer.setServices(null);   
    56.         buffaloConfigurer = null;   
    57.     }   
    58.     
    59.     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {   
    60.         return bean;   
    61.     }   
    62.   
    63.     public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {   
    64.         log.debug("find a bean:"+beanName);   
    65.         if (bean.getClass().isAnnotationPresent(Buffalo.class)) {   
    66.             Buffalo buffalo = bean.getClass().getAnnotation(Buffalo.class);   
    67.             addBuffaloBean(buffalo.value(), bean);    
    68.         }   
    69.         return bean;   
    70.     }   
    71.   
    72. }  

     

     主要思路是用FactoryBean替換原BuffaloServiceConfigurer,并掛上BeanPostProcessor的鉤子,檢測一下annotation,發現buffalo服務就添加到原BuffaloServiceConfigurer中去.

     

    3.今天我這樣配置Buffalo:

    Java代碼 復制代碼 收藏代碼
    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" xmlns:aop="http://www.springframework.org/schema/aop"  
    4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  
    5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
    6.      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
    7.      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
    8.      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">   
    9.   
    10.     <!-- Spring Annotation配置, 自動搜索組件 -->   
    11.     <context:component-scan base-package="cn.tohot.demo"/>    
    12.     <bean id="buffalo"   class="cn.tohot.common.annotation.BuffaloAnnotationServiceFactoryBean" />    
    13. </beans>  
     

     服務端的Buffalo bean 類:

    Java代碼 復制代碼 收藏代碼
    1. package cn.tohot.demo;   
    2.   
    3. import org.springframework.stereotype.Service;   
    4.   
    5. import cn.tohot.common.annotation.Buffalo;   
    6.   
    7. @Service     //聲明Spring bean,   
    8. @Buffalo("testbean"//聲明一個名為"testbean"的Buffalo service   
    9. public class BuffaloBeanTestService {   
    10.     public String run() {   
    11.         System.out.println("run");   
    12.         return "run";   
    13.     }   
    14. }  

     很簡潔,不是嗎?

    posted on 2011-08-26 09:21 xzc 閱讀(233) 評論(0)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 一本色道久久综合亚洲精品高清| 成人免费观看男女羞羞视频| 人妖系列免费网站观看| 成人看的午夜免费毛片| 久久黄色免费网站| 免费的黄色网页在线免费观看| 色拍自拍亚洲综合图区| 国产极品美女高潮抽搐免费网站 | 又黄又爽又成人免费视频| 人妻免费久久久久久久了| 亚洲色中文字幕在线播放| 亚洲精品亚洲人成在线麻豆| 国产成人亚洲精品青草天美| 久久亚洲av无码精品浪潮| 亚洲av片一区二区三区| 日本免费观看网站| 成年女人午夜毛片免费看| 无码人妻久久一区二区三区免费 | 亚洲精品无码专区2| 国产美女无遮挡免费视频网站| 无码av免费毛片一区二区| 日本人的色道免费网站| 久久国产精品免费视频| 国产精品无码永久免费888| 最好2018中文免费视频| 边摸边脱吃奶边高潮视频免费| 老子影院午夜伦不卡亚洲| 亚洲av成人片在线观看| 亚洲AV女人18毛片水真多| 亚洲国产精品无码久久98| 亚洲欧美中文日韩视频| 亚洲国产午夜精品理论片在线播放| 亚洲一级毛片在线观| 久久狠狠爱亚洲综合影院| 亚洲最大无码中文字幕| 亚洲国产精品久久久久秋霞小| 色欲aⅴ亚洲情无码AV蜜桃| 国产AV无码专区亚洲AV琪琪| 一级黄色片免费观看| 免费看少妇高潮成人片| 久久久久免费看成人影片|