栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring使用AspectJ进行AOP开发(基于注解)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring使用AspectJ进行AOP开发(基于注解)

在 Spring 中,虽然我们可以使用 XML 配置文件可以实现 AOP 开发,但如果所有的配置都集中在 XML 配置文件中,就势必会造成 XML 配置文件过于臃肿,从而给维护和升级带来一定困难。

为此,AspectJ 框架为 AOP 开发提供了一套 @AspectJ 注解。它允许我们直接在 Java 类中通过注解的方式对切面(Aspect)、切入点(Pointcut)和增强(Advice)进行定义,Spring 框架可以根据这些注解生成 AOP 代理。

关于注解的介绍如表 1 所示。

启用 @AspectJ 注解支持

在使用 @AspectJ 注解进行 AOP 开发前,首先我们要先启用 @AspectJ 注解支持。

我们可以通过以下 2 种方式来启用 @AspectJ 注解。

1)使用 Java 配置类启用

我们可以在 Java 配置类(标注了 @Configuration 注解的类)中,使用 @EnableAspectJAutoProxy 和 @ComponentScan 注解启用 @AspectJ 注解支持。

1.  @Configuration
2.  @ComponentScan(basePackages = "net.biancheng.c") //注解扫描
3.  @EnableAspectJAutoProxy //开启 AspectJ 的自动代理
4.  public class  AppConfig {
5.  }

2)基于 XML 配置启用

在 Spring 的 XML 配置文件中,添加以下内容启用 @AspectJ 注解支持。

1.  
 
2.  
3.  
 
4.  

定义切面 @Aspect

我们可以通过 @Aspect 注解将一个 Bean 定义为切面。

在启用了 @AspectJ 注解支持的情况下,Spring 会自动将 IoC 容器(ApplicationContext)中的所有使用了 @Aspect 注解的 Bean 识别为一个切面。

我们可以在 XML 配置中通过一些配置将这个类定义为一个 Bean,如下。

1.  
2.  ...
3.  

在定义完 Bean 后,我们只需要在Bean 对应的 Java 类中使用一个 @Aspect 注解,将这个 Bean 定义为一个切面,代码如下。

1.  package net.biancheng.c;

3.  import org.aspectj.lang.annotation.*;

5.  @Aspect //定义为切面
6.  public class  MyAspect {

8.  }

全注解方式定义切面

我们也可以在 Java 类上使用以下 2 个注解,使用全注解方式定义切面。

1.  package net.biancheng.c;

3.  import org.aspectj.lang.annotation.*;
4.  import org.springframework.stereotype.Component;

6.  @Component // 定义成 Bean
7.  @Aspect //定义为切面
8.  public class  MyAspect {

10.  }

在以上代码中共使用两个注解:

  • @Component 注解:将这个类的对象定义为一个 Bean;
  • @Aspect 注解:则是将这个 Bean 定义为一个切面。
定义切点 @Pointcut

在 AspectJ 中,我们可以使用 @Pointcut 注解用来定义一个切点。需要注意的是,定义为切点的方法,它的返回值类型必须为 void,示例代码如下。

1.  // 要求:方法必须是private,返回值类型为 void,名称自定义,没有参数
2.  @Pointcut("execution(*net.biancheng..*.*(..))")
3.  private void myPointCut() {
4.  }

@Pointcut 注解中有一个 value 属性,这个属性的值就是切入点表达式。有关切入点表达式的具体介绍请参考《使用AspectJ实现AOP(基于XML)》中的 execution 语法格式介绍。

值得注意的是,我除了可以通过切入点表达式(execution)直接对切点进行定义外,还可以通过切入点方法的名称来引用其他的切入点。在使用方法名引用其他切入点时,还可以使用“&&”、“||”和“!”等表示“与”、“或”、“非”的含义,示例代码如下。

1.  
4.  @Pointcut(value ="execution(* net.biancheng.c.dao.UserDao.get(..))")
5.  public void pointCut1(){

7.  }

9.  
12.  @Pointcut(value ="execution(* net.biancheng.c.dao.UserDao.delete(..))")
13.  public void pointCut2(){

15.  }

17.  
24.  @Pointcut(value ="!pointCut1() && !pointCut2()")
25.  public void pointCut3(){

27.  }

定义通知

AspectJ 为我们提供了以下 6 个注解,来定义 6 种不同类型的通知(Advice),如下表。

  ![image.png](https://img-blog.csdnimg.cn/img_convert/ee142f6924dff1f8049a06b8d35047c9.png) 以上这些通知注解中都有一个 value 属性,这个 value 属性的取值就是这些通知(Advice)作用的切点(PointCut),它既可以是切入点表达式,也可以是切入点的引用(切入点对应的方法名称),示例代码如下。 @Pointcut(value ="execution(* net.biancheng.c.dao.UserDao.get(..))") public void pointCut1(){ } @Pointcut(value ="execution(* net.biancheng.c.dao.UserDao.delete(..))") public void pointCut2(){ } @Pointcut(value ="!pointCut1() && !pointCut2()") public void pointCut3(){ } //使用切入点引用 @Before("MyAspect.pointCut3()") public void around() throws Throwable { System.out.println("环绕增强……"); } //使用切入点表达式 @AfterReturning(value = "execution(* net.biancheng.c.dao.UserDao.get(..))" ,returning = "returnValue") public void afterReturning(Object returnValue){ System.out.println("方法返回值为:"+returnValue); } 示例 下面,我们就通过一个完整的实例,来演示下如何通过注解的方式实现 AspectJ AOP 开发。
  1. 新建一个名为 my-spring-asepctj-demo2 的 Java 项目,并将以下依赖 Jar 包导入到该项目中。
    commons-logging-1.2.jar
    spring-aop-5.3.13.jar
    spring-aspects-5.3.13.jar
    spring-beans-5.3.13.jar
    spring-context-5.3.13.jar
    spring-core-5.3.13.jar
    spring-expression-5.3.13.jar
    aspectjweaver-1.9.7.jar

  2. 在 net.biancheng.c.dao 包下,创建一个名为 UserDao 的接口,代码如下。
    package net.biancheng.c.dao;
    public interface UserDao {
    public void add();
    public void delete();
    public int modify();
    public void get();
    }

  3. 在 net.biancheng.c.dao.impl 包下,创建 UserDao 的实现类 UserDaoImpl,代码如下。
    package net.biancheng.c.dao.impl;
    import net.biancheng.c.dao.UserDao;
    import org.springframework.stereotype.Component;
    @Component(“userDao”)
    public class UserDaoImpl implements UserDao {
    @Override
    public void add() {
    System.out.println(“正在执行 UserDao 的 add 方法”);
    }
    @Override
    public void delete() {
    System.out.println(“正在执行 UserDao 的 delete 方法”);
    }
    @Override
    public int modify() {
    System.out.println(“正在执行 UserDao 的 modify 方法”);
    return 1;
    }
    @Override
    public void get() {
    System.out.println(“正在执行 UserDao 的 get 方法”);
    }
    }

  4. 在 net.biancheng.c.dao.config 包下,创建一个名为 AppConfig 的配置类,代码如下。
    package net.biancheng.c.dao.config;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    @Configuration
    @ComponentScan(basePackages = “net.biancheng.c”) //注解扫描
    @EnableAspectJAutoProxy //开启 AspectJ 的自动代理
    public class AppConfig {
    }

  5. 在 net.biancheng.c 包下,创建一个名为 MyAspect 的切面类,代码如下。
    package net.biancheng.c;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.;
    import org.springframework.stereotype.Component;
    @Component // 定义成 Bean
    @Aspect //定义为切面
    public class MyAspect {
    @Before("execution( net.biancheng.c.dao.UserDao.add(…))")
    public void before(JoinPoint joinPoint) {
    System.out.println(“前置增强……” + joinPoint);
    }
    @After(“execution(* net.biancheng.c.dao.UserDao.get(…))”)
    public void after(JoinPoint joinPoint) {
    System.out.println(“最终增强……” + joinPoint);
    }
    /**

    • 将 net.biancheng.c.dao包下的 UserDao 类中的 get() 方法 定义为一个切点
      /
      @Pointcut(value = "execution( net.biancheng.c.dao.UserDao.get(…))")
      public void pointCut1() {
      }
      /**
    • 将 net.biancheng.c.dao包下的 UserDao 类中的 delete() 方法 定义为一个切点
      /
      @Pointcut(value = "execution( net.biancheng.c.dao.UserDao.delete(…))")
      public void pointCut2() {
      }
      //使用切入点引用
      @Around(“MyAspect.pointCut2()”)
      public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
      System.out.println(“环绕增强……1”);
      proceedingJoinPoint.proceed();
      System.out.println(“环绕增强……2”);
      }
      //使用切入点表达式
      @AfterReturning(value = “execution(* net.biancheng.c.dao.UserDao.modify(…))”, returning = “returnValue”)
      public void afterReturning(Object returnValue) {
      System.out.println(“后置返回增强……,方法返回值为:” + returnValue);
      }
      }
  6. 在 net.biancheng.c 包下,创建一个名为 MainApp 的类,代码如下。
    package net.biancheng.c;
    import net.biancheng.c.dao.UserDao;
    import net.biancheng.c.dao.config.AppConfig;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    public class MainApp {
    public static void main(String[] args) {
    ApplicationContext context2 = new AnnotationConfigApplicationContext(AppConfig.class);
    UserDao userDao = context2.getBean(“userDao”, UserDao.class);
    userDao.add();
    userDao.modify();
    userDao.delete();
    userDao.get();
    }
    }

  7. 执行 MainApp 中的 main() 方法,控制台输出如下。
    前置增强……execution(void net.biancheng.c.dao.UserDao.add())
    正在执行 UserDao 的 add 方法
    正在执行 UserDao 的 modify 方法
    后置返回增强……,方法返回值为:1
    环绕增强……1
    正在执行 UserDao 的 delete 方法
    环绕增强……2
    正在执行 UserDao 的 get 方法
    最终增强……execution(void net.biancheng.c.dao.UserDao.get())

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/915240.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号