概述
@AspectJ可以使用切点函数定义切点,还可以使用逻辑运算符对切点进行复合运算得到复合切点。
为了在切面中重用切点,还可以对切点进行命名,以便在其他地方引用定义过的切点。
当一个连接点匹配多个切点时,需要考虑织入顺序的问题,另外一个重要的问题是如何在增强中访问连接点上下文的信息。
示例
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster
这里仅仅展示关键代码
切面部分:
package com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; /** * * * @ClassName: PointcutComplexAspect * * @Description: 标注了@Aspect的切面 * * @author: Mr.Yang * * @date: 2017年9月9日 上午1:50:45 */ @Aspect public class PointcutComplexAspect { /** * * * @Title: matchGreetTo * * @Description: 与预算,匹配com.xgj.aop.spring.advisor.aspectJAdvance. * pointcutComplex包中所有greetTo()方法的切点 * * * @return: void */ @Before("within(com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex.*)" + " && execution(* greetTo(..))") public void matchGreetTo() { System.out.println("matchGreetTo executed,some logic is here "); } /** * * * @Title: something * * @Description: 非与预算,匹配所有的serverTo方法,且不位于WaiterOne目标类切点 * * * @return: void */ @After("!target(com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex.WaiterOne)" + " && execution(* serverTo(..))") public void something() { System.out.println("something executed,some logic is here "); } /** * * * @Title: method * * @Description: 或运算,匹配IWaiter和ISeller接口实现类所有连接点的切点 * * * @return: void */ @AfterReturning("target(com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex.IWaiter)" + " || target(com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex.ISeller)") public void method() { System.out.println("method executed,some logic is here "); } }
配置文件,Bean的初始化使用context:component-scan扫描
<?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" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- (1)声明Context命名空间以及Schema文件 (2)扫描类包以及应用注解定义的bean --> <context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex"/> <!-- 基于@AspectJ切面的驱动器 --> <aop:aspectj-autoproxy proxy-target-class="true"/> <!-- 使用了@AspectJ注解的切面类 --> <bean class="com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex.PointcutComplexAspect"/> </beans>
测试类:
package com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class PointcutComplexAspectTest { @Test public void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext( "com/xgj/aop/spring/advisor/aspectJAdvance/pointcutComplex/conf-pointcutComplex.xml"); WaiterOne waiterOne = ctx.getBean("waiterOne", WaiterOne.class); WaiterTwo waiterTwo = ctx.getBean("waiterTwo", WaiterTwo.class); SellerOne sellerOne = ctx.getBean("sellerOne", SellerOne.class); SellerTwo sellerTwo = ctx.getBean("sellerTwo", SellerTwo.class); waiterOne.greetTo("XiaoGongJiang"); System.out.println(); waiterOne.serverTo("XiaoGongJiang"); System.out.println(); waiterTwo.greetTo("XiaoGongJiang"); System.out.println(); sellerOne.greetTo("XiaoGongJiang"); System.out.println(); sellerOne.greetTo("XiaoGongJiang"); System.out.println(); } }