<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)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 激情五月亚洲色图| 老子影院午夜伦不卡亚洲| 亚洲爆乳精品无码一区二区三区 | 免费看的一级毛片| 亚洲国产理论片在线播放| 国产一区二区三区免费| 亚洲中文字幕无码久久精品1 | 成年人免费观看视频网站| 久久久亚洲欧洲日产国码aⅴ| 久久精品成人免费看| 亚洲精品高清无码视频| 青柠影视在线观看免费高清| 亚洲午夜无码片在线观看影院猛| 西西人体大胆免费视频| 亚洲裸男gv网站| 人妻在线日韩免费视频| 亚洲AV日韩AV天堂一区二区三区| 丁香花在线视频观看免费 | 日韩a级毛片免费观看| 黄色免费在线观看网址| 国产av无码专区亚洲av果冻传媒| 一级一看免费完整版毛片| 亚洲日韩小电影在线观看| 最近中文字幕完整免费视频ww| 97亚洲熟妇自偷自拍另类图片 | 亚洲国产精品成人综合色在线婷婷| 曰曰鲁夜夜免费播放视频| 丰满亚洲大尺度无码无码专线| 免费a级黄色毛片| 久操免费在线观看| 亚洲码和欧洲码一码二码三码| 亚洲情侣偷拍精品| 97视频免费在线| 精品人妻系列无码人妻免费视频| 久久久久久a亚洲欧洲AV| 一个人免费观看在线视频www| 日韩少妇内射免费播放| 亚洲乱码一二三四区乱码| 浮力影院亚洲国产第一页| 日韩精品无码区免费专区| AAA日本高清在线播放免费观看|