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

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

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

    隨筆-314  評(píng)論-209  文章-0  trackbacks-0
    轉(zhuǎn)自:http://www.iteye.com/topic/255397

    1.那即將離我遠(yuǎn)去的

    用buffalo作為我的ajax類(lèi)庫(kù)也有些歷史了,幾乎是和Spring同時(shí)開(kāi)始接觸的.

    按照官方的方式,Buffalo與Spring的集成是很簡(jiǎn)單:

        在Spring中配置一個(gè)BuffaloServiceConfigure bean,把spring托管的服務(wù)在其中聲明即可,Buffalo可以通過(guò)ServletContext得到Spring的WebApplicationContext,進(jìn)而得到所需的服務(wù):

    Java代碼 復(fù)制代碼 收藏代碼
    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>   

     

         似乎很簡(jiǎn)單,但,有沒(méi)有覺(jué)得似乎很傻?只是把Spring里已經(jīng)配置好的bean再引用一次而已,

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

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

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

         誒,等等,怎么還有大堆XML?哦?原來(lái)是Buffalo...

         Buffalo像個(gè)刺頭,傻愣愣地杵在XML里.

     

    2.于是我開(kāi)始把Buffalo也Annotation化.

     

    話說(shuō)Spring的擴(kuò)展能力還是ganggang的,一天時(shí)間,就有成果了.

    先寫(xiě)個(gè)注解:

    Java代碼 復(fù)制代碼 收藏代碼
    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擴(kuò)展接口,用于表明該類(lèi)是一個(gè)buffalo服務(wù).  
    10.  * @author tedeyang  
    11.  *  
    12.  */  
    13. @Retention(RetentionPolicy.RUNTIME)   
    14. @Target(ElementType.TYPE)   
    15. public @interface Buffalo {   
    16.     /**  
    17.      * @return 遠(yuǎn)程調(diào)用時(shí)的服務(wù)名.  
    18.      */    
    19.     String value();   
    20. }  

     

    接著再寫(xiě)Spring的擴(kuò)展

     

    Java代碼 復(fù)制代碼 收藏代碼
    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.  * 該類(lèi)作為FactoryBean可以無(wú)縫替換buffalo 2.0自帶的配置類(lèi),并使用annotation進(jìn)行配置.  
    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的鉤子,檢測(cè)一下annotation,發(fā)現(xiàn)buffalo服務(wù)就添加到原BuffaloServiceConfigurer中去.

     

    3.今天我這樣配置Buffalo:

    Java代碼 復(fù)制代碼 收藏代碼
    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配置, 自動(dòng)搜索組件 -->   
    11.     <context:component-scan base-package="cn.tohot.demo"/>    
    12.     <bean id="buffalo"   class="cn.tohot.common.annotation.BuffaloAnnotationServiceFactoryBean" />    
    13. </beans>  
     

     服務(wù)端的Buffalo bean 類(lèi):

    Java代碼 復(fù)制代碼 收藏代碼
    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"//聲明一個(gè)名為"testbean"的Buffalo service   
    9. public class BuffaloBeanTestService {   
    10.     public String run() {   
    11.         System.out.println("run");   
    12.         return "run";   
    13.     }   
    14. }  

     很簡(jiǎn)潔,不是嗎?

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

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲国产一区二区三区青草影视| 亚洲天堂久久精品| 亚洲真人无码永久在线| 亚洲精品无码久久久久sm| 亚洲综合精品一二三区在线| 亚洲精品国产电影午夜| 亚洲熟妇无码一区二区三区| 天天综合亚洲色在线精品| 国产免费一区二区三区免费视频| 国产亚洲免费的视频看| 成人免费一级毛片在线播放视频 | 色片在线免费观看| 午夜免费福利影院| 亚洲人成电影网站国产精品| 亚洲AV永久无码精品| 亚洲国产成人精品激情| 免费无遮挡无遮羞在线看| 欧洲人成在线免费| 好爽…又高潮了免费毛片| 国产日产亚洲系列| 亚洲一级黄色大片| 人妻仑乱A级毛片免费看| 91精品视频在线免费观看| 国产成人综合久久精品免费| 亚洲VA中文字幕无码一二三区| 国产成人精品日本亚洲网址| a一级毛片免费高清在线| 91频在线观看免费大全| 亚洲精品麻豆av| 亚洲专区中文字幕| 黄视频在线观看免费| 亚洲成在人线aⅴ免费毛片| 久久久久无码专区亚洲av| 亚洲乱码一二三四区麻豆| WWW国产成人免费观看视频| 97在线观免费视频观看| 亚洲精品你懂的在线观看| 亚洲欧洲无码一区二区三区| 国产麻豆一精品一AV一免费| 国产老女人精品免费视频| 久久亚洲AV成人无码国产|