現在你可以為你的應用系統(tǒng)創(chuàng)建可重復使用的切入點了。Spring支持在這些切入點上進行操作-合并與交叉-來創(chuàng)建新的切入點
。只有當所有切入點都匹配時交叉集合才匹配,任何一個切入點匹配都會使合并集合匹配。為了對2個Pointcut對象進行合并,必須使用Pointcuts類。例如:
Pointcut union = Pointcuts.union(pointcut1,pointcut2);
這種方式的一個缺點是它需要通過編碼來實現。
package com.wyq.spring.base.aopinstance;
import java.util.List;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.Pointcuts;
/**
* @author 作者
* @version 創(chuàng)建時間:2009-11-6 下午02:18:03
* 類說明
*/
public class UnionPointcut implements Pointcut {
//聲明合并的Pointcut實例
private Pointcut delegate;
public Pointcut getDelegate() {
return delegate;
}
public void setDelegate(Pointcut delegate) {
this.delegate = delegate;
}
//委托給Pointcut的方法
public ClassFilter getClassFilter() {
return getDelegate().getClassFilter();
}
public MethodMatcher getMethodMatcher() {
return getDelegate().getMethodMatcher();
}
//創(chuàng)建組合切入點
public void setPointcuts(List pointcuts){
if(pointcuts == null || pointcuts.size()==0){
System.out.println("沒有要組合的切入點");
}
delegate = (Pointcut)pointcuts.get(0);
for(int i=1;i<pointcuts.size();i++){
Pointcut pointcut = (Pointcut)pointcuts.get(i);
delegate = Pointcuts.union(delegate,pointcut);
}
}
}
映射文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="frequentCustomerAdvice" class="com.wyq.spring.common.aopinstance.namemachmethodpointcut.PrequentCustomerAdvice"></bean>
<bean id="queryInterceptor" class="com.wyq.spring.common.aopinstance.namemachmethodpointcut.QueryInterceptor"></bean>
<bean id="unionpointcut" class="com.wyq.spring.common.aopinstance.composablepointcut.UnionPointcut">
<property name="delegate">
<ref bean="frequentCustomerPointcutAdvisor"/>
</property>
<property name="pointcuts">
<list>
<ref bean="queryPointCutAdvisor"/>
</list>
</property>
</bean>
<bean id="frequentCustomerPointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedName">
<value>order*</value>
</property>
<property name="advice">
<ref bean="frequentCustomerAdvice"/>
</property>
</bean>
<bean id="queryPointCutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="pattern">
<value>.*get.+By.+</value>
</property>
<property name="advice">
<ref bean="queryInterceptor"/>
</property>
</bean>
</beans>
posted on 2009-11-06 14:48
王永慶 閱讀(163)
評論(0) 編輯 收藏 所屬分類:
SPRING