见文生义,其实就是偷懒。抽取你还不懂吗?
正文:
当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽
取后的切点表达式。
其实是在配置文件中:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="target" class="com.bihu.aop.Target"></bean>
<bean id="myAspect" class="com.bihu.aop.MyAspect"></bean>
<aop:config>
<!--引用myAspect的Bean为切面对象-->
<aop:aspect ref="myAspect">
<aop:before method="before" pointcut="execution(* com.bihu.aop.*.*(..))"/>
<aop:after-returning method="afterReturning" pointcut="execution(* com.bihu.aop.*.*(..))"/>
<aop:around method="around" pointcut="execution(* com.bihu.aop.*.*(..))"/>
<aop:after method="after" pointcut="execution(* com.bihu.aop.*.*(..))"/>
</aop:aspect>
</aop:config>
</beans>
我现在上面四个切点表达式都是一样的 ,我可以这样:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="target" class="com.bihu.aop.Target"></bean>
<bean id="myAspect" class="com.bihu.aop.MyAspect"></bean>
<aop:config>
<!--引用myAspect的Bean为切面对象-->
<aop:aspect ref="myAspect">
<!--对切点表达式的抽取-->
<aop:pointcut id="myPointcutExpression" expression="execution(* com.bihu.aop.*.*(..))"/>
<!--下面配置切点表达式 以及 切点的(通知)增强方法-->
<aop:before method="before" pointcut-ref="myPointcutExpression"/>
<aop:after-returning method="afterReturning" pointcut-ref="myPointcutExpression"/>
<aop:around method="around" pointcut-ref="myPointcutExpression"/>
<aop:after method="after" pointcut-ref="myPointcutExpression"/>
</aop:aspect>
</aop:config>
</beans>
自己对比一下 会发现不一样的就是在
pointcut-ref 值是 你抽取 切点表达式的 id。
作者:咸瑜