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); }
<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>
<bean id="dwrInterceptor" class="net.jf.ajax.iterceptor.MainInterceptor"> <property name="interceptors"> <list> <ref bean="test"/> </list> </property> </bean> <!--其中一个子拦截器的实现--> <bean id="test" class="net.jf.ajax.iterceptor.TestInterceptor"/> </beans>
就 这样,在配置DWR的配置文件时,配置<creator>时使用Spring的Creator就可以直接使用上面的Service了。当 DWR远程请求时,在配置范围内的方法的调用都会被主拦截器拦截,并且遍历、执行所有子拦截器。原有的Service不需要改动,只需要多加一个 Spring的配置文件,将原有的Service再加一层Aop的轻纱。
这是一种实现方法。如果有别的方法让DWR更安全、有效,请一定告知。:)
| 第1页: 第1页 |