Spring-AOP @AspectJ进阶之切点复合运算

简介: Spring-AOP @AspectJ进阶之切点复合运算

概述


@AspectJ可以使用切点函数定义切点,还可以使用逻辑运算符对切点进行复合运算得到复合切点。


为了在切面中重用切点,还可以对切点进行命名,以便在其他地方引用定义过的切点。


当一个连接点匹配多个切点时,需要考虑织入顺序的问题,另外一个重要的问题是如何在增强中访问连接点上下文的信息。


示例

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster


aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcwOTA5MTQyODM1Mjcy.png


这里仅仅展示关键代码

切面部分:

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();
  }
}


相关文章
|
3月前
|
XML Java API
Spring AOP切点和通知机制的深度解析
Spring AOP切点和通知机制的深度解析
70 4
|
4月前
|
Java 开发者 Spring
Spring AOP的切点是通过使用AspectJ的切点表达式语言来定义的。
【5月更文挑战第1天】Spring AOP的切点是通过使用AspectJ的切点表达式语言来定义的。
56 5
|
4月前
Spring5源码(31)-基于@AspectJ的AOP
Spring5源码(31)-基于@AspectJ的AOP
42 0
|
4月前
|
XML Java 数据格式
Spring-AOP @AspectJ语法基础
Spring-AOP @AspectJ语法基础
74 0
|
Java Spring
java202304java学习笔记第六十四天-ssm-aop切点表达式的写法1
java202304java学习笔记第六十四天-ssm-aop切点表达式的写法1
57 0
|
测试技术
Spring-AOP @AspectJ进阶之绑定抛出的异常
Spring-AOP @AspectJ进阶之绑定抛出的异常
80 0
Spring-AOP @AspectJ进阶之绑定连接点方法的返回值
Spring-AOP @AspectJ进阶之绑定连接点方法的返回值
56 0
Spring-AOP @AspectJ进阶之绑定类注解对象
Spring-AOP @AspectJ进阶之绑定类注解对象
62 0
|
10天前
Micronaut AOP与代理机制:实现应用功能增强,无需侵入式编程的秘诀
AOP(面向切面编程)能够帮助我们在不修改现有代码的前提下,为应用程序添加新的功能或行为。Micronaut框架中的AOP模块通过动态代理机制实现了这一目标。AOP将横切关注点(如日志记录、事务管理等)从业务逻辑中分离出来,提高模块化程度。在Micronaut中,带有特定注解的类会在启动时生成代理对象,在运行时拦截方法调用并执行额外逻辑。例如,可以通过创建切面类并在目标类上添加注解来记录方法调用信息,从而在不侵入原有代码的情况下增强应用功能,提高代码的可维护性和可扩展性。
28 1