SpringAOP 切面

切面 = 增强 + 切点(连接点为目标类的特定方法)
切面 = 增强(连接点为目标类的所有方法)
增强包含连接点的方位和织入代码,切点包含执行点信息(哪些类的哪些方法)

[b]切点[/b]
org.springframework.aop.Pointcut接口描述切点
Pointcut由ClassFilter和MethodMatcher构成
[i][b]ClassFilter[/b][/i]
定位到特定类上
matches(Class clazz)用来判别被检测的类是否匹配过滤条件
[i][b]MethodMatcher[/b][/i]
定位到特定方法上
matches(Method m,Class targetClass)静态方法匹配器,匹配方法签名,仅一次
matches(Method m,Class targetClass,Object[] args)动态方法匹配器,在运行期检查方法入参的值,每次调用都匹配
isRuntime()返回false表示静态方法匹配器,返回true表示动态方法匹配器

[b]切点类型[/b]
[i][b]静态方法切点[/b][/i]
抽象基类org.springframework.aop.support.StaticMethodMatcherPointcut默认匹配所有类
子类NameMatchMethodPointcut字符串匹配
子类AbstractRegexpMethodPointcut正则匹配
[i][b]动态方法切点[/b][/i]
抽象基类org.springframework.aop.support.DynamicMethodMatcherPointcut默认匹配所有类,已过时,可用DefaultPointCutAdvice和DynamicMethodMatcherPointcut替代。
[i][b]注解切点[/b][/i]
org.springframework.aop.support.annotation.AnnotationMatchingPointcut支持在Bean中直接通过JDK5.0注解标签定义的切点
[i][b]表达式切点[/b][/i]
org.springframework.aop.support.ExpressionPointcut支持AspectJ切点表达式
[i][b]流程切点[/b][/i]
org.springframework.aop.support.ControlFlowPointcut根据程序执行的堆栈信息查看目标方法是否由某一个方法直接或间接发起调用,以此判断是否为匹配的连接点。
[i][b]复合切点[/b][/i]
org.springframework.aop.support.ComposablePointcut可创建多个切点

[b]切面类型[/b]
[i][b]Advice[/b][/i]
一般切面,仅包含Advice,连接点是目标类的所有方法。
[i][b]PointcutAdvice[/b][/i]
切点切面,包含Advice和Pointcut,连接点是目标类的特定方法。
[i][b]IntroductionAdvice[/b][/i]
引介切面,包含IntroductionInterceptor和Pointcut,类级别。

[b]静态普通方法名匹配切面[/b]
[i][b]定义切点[/b][/i]
public class GreetingAdvisor extends StaticMethodMatcherPointcut{
public boolean matches(Method method, Class clazz){//切点方法
return "greetTo".equals(method.getName());
}
public ClassFilter getClassFilter(){//切点类
return new ClassFilter(){
public boolean matcher(Class clazz){
return Waiter.class.isAssignableFrom(clazz);
}
}
}
}
[i][b]定义增强[/b][/i]
public class GreetingBeforeAdvice implements MethodBeforeAdvice{
public void before(Method method, Object[] args, Object obj) throws Throwable{
//...
}
}
[i][b]配置切面[/b][/i]
<!-- 目标类 -->
<bean id="waiterTarget" class="com.smart.advisor.Waiter" />
<bean id="sellerTarget" class="com.smart.advisor.Seller" />
<!-- 前置增强 -->
<bean id="beforeAdvice" class="com.smart.advisor.GreetingBeforeAdvice" />
<!-- 切面,advice-增强,classFilter-类匹配过滤器,order-切面织入时的顺序 -->
<bean id="greetingAdvisor" class="com.smart.advisor.GreetingAdvisor"
p:advice-ref="beforeAdvice"/>
<!-- 父bean,定义公共配置信息 -->
<bean id="parent" abstract="true"
class="org.springframework.ProxyFactoryBean"
p:interceptorNames="greetingAdvisor"
p:proxyTargetClass="true"/>
<!-- 代理bean -->
<bean id="waiter" parent="parent" p:tartget-ref="waiterTarget"/>
<bean id="seller" parent="parent" p:tartget-ref="sellerTarget"/>

[b]静态正则表达式方法匹配切面[/b]
<!-- 匹配目标类方法的全限定名 -->
<bean id="regexpAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"
p:advice-ref="beforeAdvice">
<property name="patterns">
<list><value>.*greet.*</value></list>
</property>
</bean>
<bean id="parent" class="org.springframework.ProxyFactoryBean"
p:interceptorNames="greetingAdvisor"
p:target-ref="waiterTarget"
p:proxyTargetClass="true"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值