轉自:
http://www.iteye.com/topic/255397
1.那即將離我遠去的
用buffalo作為我的ajax類庫也有些歷史了,幾乎是和Spring同時開始接觸的.
按照官方的方式,Buffalo與Spring的集成是很簡單:
在Spring中配置一個BuffaloServiceConfigure bean,把spring托管的服務在其中聲明即可,Buffalo可以通過ServletContext得到Spring的WebApplicationContext,進而得到所需的服務:
- <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>
似乎很簡單,但,有沒有覺得似乎很傻?只是把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的,一天時間,就有成果了.
先寫個注解:
- 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擴展接口,用于表明該類是一個buffalo服務.
* @author tedeyang
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Buffalo {
/**
* @return 遠程調用時的服務名.
*/
String value();
}
接著再寫Spring的擴展
-
-
-
- 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;
/**
* 該類作為FactoryBean可以無縫替換buffalo 2.0自帶的配置類,并使用annotation進行配置.
* @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的鉤子,檢測一下annotation,發現buffalo服務就添加到原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配置, 自動搜索組件 -->
- <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配置, 自動搜索組件 -->
<context:component-scan base-package="cn.tohot.demo"/>
<bean id="buffalo" class="cn.tohot.common.annotation.BuffaloAnnotationServiceFactoryBean" />
</beans>
服務端的Buffalo bean 類:
- 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") //聲明一個名為"testbean"的Buffalo service
public class BuffaloBeanTestService {
public String run() {
System.out.println("run");
return "run";
}
}
很簡潔,不是嗎?
posted on 2011-08-26 09:21
xzc 閱讀(233)
評論(0) 編輯 收藏