轉(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ù):
- <bean name="buffaloConfigBean"
- class="net.buffalo.service.BuffaloServiceConfigurer">
- <property name="services">
- <map>
- <entry key="springSimpleService">
- <ref bean="systemService" />
- </entry>
- <entry key="springSimpleService2">
- <ref bean="systemService2" />
- </entry>
- </map>
- </property>
- </bean>
<bean name="buffaloConfigBean"
class="net.buffalo.service.BuffaloServiceConfigurer">
<property name="services">
<map>
<entry key="springSimpleService">
<ref bean="systemService" />
</entry>
<entry key="springSimpleService2">
<ref bean="systemService2" />
</entry>
</map>
</property>
</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è)注解:
- package cn.tohot.common.annotation;
-
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
-
-
-
-
-
-
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.TYPE)
- public @interface Buffalo {
-
-
-
- String value();
- }
package cn.tohot.common.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* buffalo擴(kuò)展接口,用于表明該類(lèi)是一個(gè)buffalo服務(wù).
* @author tedeyang
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Buffalo {
/**
* @return 遠(yuǎn)程調(diào)用時(shí)的服務(wù)名.
*/
String value();
}
接著再寫(xiě)Spring的擴(kuò)展
-
-
-
- package cn.tohot.common.annotation;
-
- import java.util.HashMap;
-
- import net.buffalo.service.BuffaloServiceConfigurer;
-
- import org.apache.log4j.Logger;
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.DisposableBean;
- import org.springframework.beans.factory.FactoryBean;
- import org.springframework.beans.factory.InitializingBean;
- import org.springframework.beans.factory.config.BeanPostProcessor;
-
-
-
-
-
-
- @SuppressWarnings("unchecked")
- public class BuffaloAnnotationServiceFactoryBean implements FactoryBean, InitializingBean, DisposableBean, BeanPostProcessor {
- private static final Logger log = Logger.getLogger(BuffaloAnnotationServiceFactoryBean.class);
-
- private BuffaloServiceConfigurer buffaloConfigurer = null;
-
- public BuffaloAnnotationServiceFactoryBean() {
- buffaloConfigurer = new BuffaloServiceConfigurer();
- buffaloConfigurer.setServices(new HashMap());
- }
-
- private void addBuffaloBean(String buffaloServiceName,Object bean) {
- buffaloConfigurer.getServices().put(buffaloServiceName, bean);
- log.info("Add a buffalo service :"+buffaloServiceName);
- }
-
- public Object getObject() throws Exception {
- return this.buffaloConfigurer;
- }
-
- public Class getObjectType() {
- return BuffaloServiceConfigurer.class;
- }
-
- public boolean isSingleton() {
- return true;
- }
-
- public void afterPropertiesSet() throws Exception {
- }
-
- public void destroy() throws Exception {
- if (buffaloConfigurer != null)
- buffaloConfigurer.setServices(null);
- buffaloConfigurer = null;
- }
-
- public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
- return bean;
- }
-
- public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
- log.debug("find a bean:"+beanName);
- if (bean.getClass().isAnnotationPresent(Buffalo.class)) {
- Buffalo buffalo = bean.getClass().getAnnotation(Buffalo.class);
- addBuffaloBean(buffalo.value(), bean);
- }
- return bean;
- }
-
- }
/**
*
*/
package cn.tohot.common.annotation;
import java.util.HashMap;
import net.buffalo.service.BuffaloServiceConfigurer;
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* 該類(lèi)作為FactoryBean可以無(wú)縫替換buffalo 2.0自帶的配置類(lèi),并使用annotation進(jìn)行配置.
* @author tedeyang
*
*/
@SuppressWarnings("unchecked")
public class BuffaloAnnotationServiceFactoryBean implements FactoryBean, InitializingBean, DisposableBean, BeanPostProcessor {
private static final Logger log = Logger.getLogger(BuffaloAnnotationServiceFactoryBean.class);
private BuffaloServiceConfigurer buffaloConfigurer = null;
public BuffaloAnnotationServiceFactoryBean() {
buffaloConfigurer = new BuffaloServiceConfigurer();
buffaloConfigurer.setServices(new HashMap());
}
private void addBuffaloBean(String buffaloServiceName,Object bean) {
buffaloConfigurer.getServices().put(buffaloServiceName, bean);
log.info("Add a buffalo service :"+buffaloServiceName);
}
public Object getObject() throws Exception {
return this.buffaloConfigurer;
}
public Class getObjectType() {
return BuffaloServiceConfigurer.class;
}
public boolean isSingleton() {
return true;
}
public void afterPropertiesSet() throws Exception {
}
public void destroy() throws Exception {
if (buffaloConfigurer != null)
buffaloConfigurer.setServices(null);
buffaloConfigurer = null;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
log.debug("find a bean:"+beanName);
if (bean.getClass().isAnnotationPresent(Buffalo.class)) {
Buffalo buffalo = bean.getClass().getAnnotation(Buffalo.class);
addBuffaloBean(buffalo.value(), bean);
}
return bean;
}
}
主要思路是用FactoryBean替換原BuffaloServiceConfigurer,并掛上BeanPostProcessor的鉤子,檢測(cè)一下annotation,發(fā)現(xiàn)buffalo服務(wù)就添加到原BuffaloServiceConfigurer中去.
3.今天我這樣配置Buffalo:
- <?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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http:
- http:
- http:
- http:
-
- <!-- Spring Annotation配置, 自動(dòng)搜索組件 -->
- <context:component-scan base-package="cn.tohot.demo"/>
- <bean id="buffalo" class="cn.tohot.common.annotation.BuffaloAnnotationServiceFactoryBean" />
- </beans>
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- Spring Annotation配置, 自動(dòng)搜索組件 -->
<context:component-scan base-package="cn.tohot.demo"/>
<bean id="buffalo" class="cn.tohot.common.annotation.BuffaloAnnotationServiceFactoryBean" />
</beans>
服務(wù)端的Buffalo bean 類(lèi):
- package cn.tohot.demo;
-
- import org.springframework.stereotype.Service;
-
- import cn.tohot.common.annotation.Buffalo;
-
- @Service
- @Buffalo("testbean")
- public class BuffaloBeanTestService {
- public String run() {
- System.out.println("run");
- return "run";
- }
- }
package cn.tohot.demo;
import org.springframework.stereotype.Service;
import cn.tohot.common.annotation.Buffalo;
@Service //聲明Spring bean,
@Buffalo("testbean") //聲明一個(gè)名為"testbean"的Buffalo service
public class BuffaloBeanTestService {
public String run() {
System.out.println("run");
return "run";
}
}
很簡(jiǎn)潔,不是嗎?