實(shí)現(xiàn)攔截功能的類有:
一、MainInteceptor,主攔截器,所以DWR的遠(yuǎn)程調(diào)用都會(huì)被攔截,當(dāng)然, 調(diào)用是細(xì)到方法級(jí)的,可配置的,該類實(shí)現(xiàn)了Spring AOP的MethodBeforeAdvice接口,該類有一個(gè)集合成員變量,成員為IInteceptor。
二、IInteceptor,是一個(gè)接口,僅有一個(gè)execute(AopContext context)函數(shù)。該接口是攔截器(與前面的主攔截器不同,本接口定義的攔截器是可以由用戶去實(shí)現(xiàn),并且可以有多個(gè))。實(shí)現(xiàn)接口只需要實(shí)現(xiàn)方法。這些 攔截器會(huì)被主攔截器回調(diào)。 比如要實(shí)現(xiàn)一個(gè)身份驗(yàn)證的攔截,SecuityInteceptor,在配置文件中把這個(gè)攔截器設(shè)置為主攔截器的屬性即可獲得回調(diào)。
三、AopContext,Aop上下文。在主攔截器調(diào)用IInteceptor的對(duì)象時(shí),把這個(gè)上下文對(duì)象作為參數(shù)來調(diào)用子攔截器。從該上下文可獲得一系列信息,如Httpsession,HttpRequest等。甚至你可以自已設(shè)置屬性。
下面看一些代碼片斷:
MainInteceptor:
private List<IInterceptor> interceptors;//定義一系列的子攔截器
public void setInterceptors(List<IInterceptor> interceptors) {
this.interceptors = interceptors;
}
在before(Method method, Object[] params, Object target)方法里:
WebContext ctx = WebContextFactory.get();//唯一被DWR污染的地方
HttpSession session = ctx.getSession();
AopContext context = new AopContext(); context.setSession(session);
for(Iterator it = interceptors.iterator(); it.hasNext();){
IInterceptor interceptor = (IInterceptor) it.next();
interceptor.execute(context);
}
IInterceptor:
public interface IInterceptor {
public void execute(AopContext context);
}
AopContext就不必貼出來了, 隨自已定義些什么屬性,不過就內(nèi)置了一個(gè)Map,用來保存數(shù)據(jù)罷了。
下面來看看配置文件:
<beans>
<!--將要暴露給DWR的Service-->
<bean id="bookManager" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>net.jf.Ajax.business.BookManager</value>
</property>
<property name="target">
<ref local="bookManagerImpl"/>
</property>
<property name="interceptorNames">
<list>
<value>dwrAdvisor</value>
</list>
</property>
</bean>
<bean id="bookManagerImpl" class="net.jf.ajax.business.impl.BookManagerImpl"/>
<!--裝配器?假如看不懂,先看看Spring的Aop吧 :P-->
<bean id="dwrAdvisor" class="org.springframework.aop.support.RegeXPMethodPointcutAdvisor">
<property name="advice">
<ref local="dwrInterceptor"/>
</property>
<property name="patterns">
<list>
<value>.*.*</value>
</list>
</property>
</bean>
<!--主攔截器,給它設(shè)置子攔截器-->
<bean id="dwrInterceptor" class="net.jf.ajax.iterceptor.MainInterceptor">
<property name="interceptors">
<list>
<ref bean="test"/>
</list>
</property>
</bean>
<!--其中一個(gè)子攔截器的實(shí)現(xiàn)-->
<bean id="test" class="net.jf.ajax.iterceptor.TestInterceptor"/>
</beans>
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注