Spring Aop:三、使用 AspectJ 框架实现 Spring AOP

本文参考实验楼教程:https://www.shiyanlou.com/courses/578/learning/?id=1940

AspectJ是基于注解(Annotation)的,所以需要JDK5.0版本以上。本文实验环境延用之Spring Aop:一、四种advice 的实验环境。

AspectJ支持的注解类型如下:

  • @Before
  • @After
  • @AfterReturning
  • @AfterThrowing
  • @Around
1、准备工作

首先定义一个简单的bean,CustomerBo实现了ICustomerBo接口。ICustomerBo接口代码如下:

package com.shiyanlou.spring.aop.aspectj;

/**
 * Created by Administrator on 2019/11/2.
 */
public interface ICustomerBo {

    void addCustomer();
    void deleteCustomer();
    String addCustomerReturnValue();
    void addCustomerThrowException() throws Exception;
    void addCustomerAround(String name);

}

CustomerBo.java代码如下:

package com.shiyanlou.spring.aop.aspectj;

/**
 * Created by Administrator on 2019/11/2.
 */
public class CustomerBo implements ICustomerBo {
    @Override
    public void addCustomer() {
        System.out.println("addCustomer() is running...");
    }

    @Override
    public void deleteCustomer() {
        System.out.println("deleteCustomer() is running...");
    }

    @Override
    public String addCustomerReturnValue() {
        System.out.println("addCustomerReturnValue() is running...");
        return "abc";
    }

    @Override
    public void addCustomerThrowException() throws Exception {
        System.out.println("addCustomerThrowException() is running...");
        throw new Exception("Generic Error!");
    }

    @Override
    public void addCustomerAround(String name) {
        System.out.println("addCustomerAround() is running, args: " + name);
    }
}


2、简单的AspectJ,Advice和Pointcut结合在一块的

首先在没有引入AspectJ之前,Advice和Pointcut是混在一块的,步骤如下:

  1. 创建一个Aspect类
  2. 配置Spring配置文件

由于接下来要使用aspectj的jar包,首先要添加maven依赖,需要在pom.xml中添加:

    
      org.aspectj
      aspectjweaver
      1.9.2
    
    
      org.aspectj
      aspectjtools
      1.9.2
    
    
      org.aspectj
      aspectjrt
      1.9.2
    

注:这在之前已经添加过了,这里这是说明他们的用途。



创建AspectJ类,LoggingAspect.java如下:

package com.shiyanlou.spring.aop.aspectj;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * Created by Administrator on 2019/11/2.
 */

@Aspect
public class LoggingAspect {

    @Before("execution(public * com.shiyanlou.spring.aop.aspectj.CustomerBo.addCustomer(..))")
    public void logBefore(JoinPoint joinPoint){
        System.out.println("logBefore() is running...");
        System.out.println("wei:" + joinPoint.getSignature().getName());
        System.out.println("***********");
    }

    @After("execution(public * com.shiyanlou.spring.aop.aspectj.CustomerBo.deleteCustomer(..))")
    public void logAfter(JoinPoint joinPoint){
        System.out.println("logAfter() is running...");
        System.out.println("wei:" + joinPoint.getSignature().getName());
        System.out.println("***********");
    }
}

解释:

  1. 必须使用@AspectLoggingAspect之前声明,以便被框架扫描到;
  2. 此例中Advice和Poincut结合在一起,LoggingAspect类中的logBeforelogAfter即为Advice,要注入的代码,即它们上方的代码为Pointcut表达式,即定义了切入点。上例中@Before中代码表示在执行com.shiyanlou.spring.aop.aspectj.CustomerBoaddCustomer方法前注入logBefore代码;
  3. 需要在LoggingAspect类的方法前加上@Before@After等注释;
  4. execution(public * com.shiyanlou.spring.aop.aspectj.CustomerBo.addCustomer(..))是Aspect的Pointcut表达式:
  • *代表任意返回类型
  • 后面定义要拦截的方法名(全路径:包名.类名.方法名)
  • (..)代表匹配参数:任意多个任意类型的参数,若确认没有参数可以写();还可以用(*)代表任意一个类型的参数,(*,String)表示匹配两个参数,第一个任意类型,第二个必须是String类型。
  1. AspectJ表达式可以对整个包定义,例如:execution(public * com.shiyanlou.spring.aop.aspectj.*.*(..))表示切入点是com.shiyanlou.spring.aop.aspectj整个包的所有类的所有方法。



配置SpringAopAspectJ.xml文件如下:




    

    
    

代表启动AspectJ支持,这样Spring会自动寻找@Aspect注释过的类,其他配置一致;

执行App.java,如下:

package com.shiyanlou.spring;

import com.shiyanlou.spring.aop.advice.CustomerService;
import com.shiyanlou.spring.aop.aspectj.CustomerBo;
import com.shiyanlou.spring.aop.aspectj.ICustomerBo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class App 
{
    private static ApplicationContext context;

    public static void main( String[] args ) {

        context = new ClassPathXmlApplicationContext("SpringAopAspectJ.xml");

        ICustomerBo cust = (ICustomerBo) context.getBean("customerBo");

        cust.addCustomer();
        System.out.println("-----------------------");
        cust.deleteCustomer();
    }
}

执行结果如下:

logBefore() is running...
wei:addCustomer
***********
addCustomer() is running...
-----------------------
deleteCustomer() is running...
logAfter() is running...
wei:deleteCustomer
***********


3、将Advice和Pointcut 分开

需要三步骤:

  1. 创建Pointcut
  2. 创建Advice
  3. 配置Spring的配置文件

1)、定义Pointcut
创建PointcutsDefinitions.java,如下:

package com.shiyanlou.spring.aop.aspectj;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

/**
 * Created by Administrator on 2019/11/2.
 */
@Aspect
public class PointcutsDefinitions {

    @Pointcut("execution(public * com.shiyanlou.spring.aop.aspectj.CustomerBo.*(..))")
    public void customreLog(){
    }
}

解释:

  1. 类声明之前加上@Aspect注释,一遍被框架扫描到;
  2. @Pointcut定义了切入点声明,指定需要注入代码的位置(即在哪个类的哪个方法中注入额外代码),上例中指定的切入点为CustomerBo.java类中的所有方法;但是往往实际应用中,我们需要切入的是一整个业务逻辑层(简单点说就是某个包),例如@Pointcut("execution(public * com.shiyanlou.spring.aop.aspectj.*.*(..))"),表示的是com.shiyanlou.spring.aop.aspectj 包中所有类的所有方法。
  3. 方法customreLog只是一个签名,在Advice中可以用此签名代替切入点表达式,多以不需要写方法体,只起到助记功能,例如此处代表操作CustomerBo类中的切入点。

2)、修改LoggingAspect.java

package com.shiyanlou.spring.aop.aspectj;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * Created by Administrator on 2019/11/2.
 */

@Aspect
public class LoggingAspect {

@Before("com.shiyanlou.spring.aop.aspectj.PointcutsDefinitions.customreLog()")
    public void logBefore(JoinPoint joinPoint){
        System.out.println("logBefore() is running...");
        System.out.println("wei:" + joinPoint.getSignature().getName());
        System.out.println("***********");
    }

    @After("com.shiyanlou.spring.aop.aspectj.PointcutsDefinitions.customreLog()")
    public void logAfter(JoinPoint joinPoint){
        System.out.println("logAfter() is running...");
        System.out.println("wei:" + joinPoint.getSignature().getName());
        System.out.println("***********");
    }
}

解释:

  1. @Before@After注释使用PointcutsDefinitions中的方法签名代替Pointcut表达式找到相应的切入点, 即通过签名找到PointcutsDefinitions方法customreLog前定义的Pointcut表达式;
  2. 对于PointcutsDefinitions来说,它的主要职责是定义Pointcut即切入点,可以在其中定义多个切入点,并且使用便于记忆的方法签名表示;
  3. 单独定义Pointcut表达式的好处有,一是使用了有意义的方法名,二是Pointcut表达式可以被多个Advice共享,修改一处其他所有使用的地方均被修改。

3)、配置Spring配置文件
配置的SpringAopAspectJ.xml如下:




    

    
    

App.java不变,运行如下:

logBefore() is running...
wei:addCustomer
***********
addCustomer() is running...
logAfter() is running...
wei:addCustomer
***********
-----------------------
logBefore() is running...
wei:deleteCustomer
***********
deleteCustomer() is running...
logAfter() is running...
wei:deleteCustomer
***********

你可能感兴趣的:(Spring Aop:三、使用 AspectJ 框架实现 Spring AOP)