博客
关于我
【Spring AOP】AspectJ开发
阅读量:321 次
发布时间:2019-03-04

本文共 5322 字,大约阅读时间需要 17 分钟。

AspectJ 开发指南

AOP 操作准备 — AspectJ

Spring框架一般都是基于AspectJ实现AOP操作。AspectJ 不是 Spring 组成部分,属于独立 AOP 框架,通常与 Spring 框架一起使用,用于实现 AOP 操作。

What is AspectJ?

AspectJ 是一个专注于实现 AOP(面向切面编程)的框架,不是 Spring 的一部分。它通常与 Spring 结合使用,用于对应用程序中的某些方面(如日志记录、事务管理等)进行增强。

How to use it?

基于 AspectJ 实现 AOP 操作的方式有以下两种:

  • 基于 XML 配置文件方式:通过配置 XML 文件,定义切入点和切面逻辑。
  • 基于注解方式:通过定义注解,简化配置,实现增强逻辑。
  • AspectJ 基于注解方式操作实现 AOP

    第一步:在项目中引入 AspectJ 相关依赖

    确保项目中引入了 AspectJ 和 Spring 的相关依赖库,以支持注解驱动的 AOP 操作。

    第二步:创建被增强的类

    package AspectJ.anno;import org.springframework.stereotype.Component;/** * 被增强的类 */@Componentpublic class User {    public void add() {        System.out.println("add......");    }}

    第三步:创建增强类(编写增强逻辑)

    package AspectJ.anno;import org.springframework.stereotype.Component;/** * 增强的类 */@Componentpublic class UserProxy {    public void before() {        System.out.println("before......");    }}

    第四步:进行通知的配置

  • 在 Spring 配置文件中开启注解扫描
    1. 使用注解创建 User 和 UserProxy 对象
    2. package AspectJ.anno;import org.springframework.stereotype.Component;/** * 被增强的类 */@Componentpublic class User {    public void add() {        System.out.println("add......");    }}package AspectJ.anno;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;/** * 增强的类 */@Componentpublic class UserProxy {    @Before(value = "execution(* AspectJ.anno.User.add(..))")    public void before() {        System.out.println("before......");    }}
      1. 在增强类上面添加注解 @Aspect
      2. package AspectJ.anno;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;/** * 增强的类 */@Component@Aspectpublic class UserProxy {    @Before(value = "execution(* AspectJ.anno.User.add(..))")    public void before() {        System.out.println("before......");    }}
        1. 在 Spring 配置文件中开启生成代理对象
        2. 配置不同类型的通知

          1. 环绕通知(最常见的)

          package AspectJ.anno;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Around;import org.springframework.stereotype.Component;/** * 增强的类 */@Component@Aspectpublic class UserProxy {    @Around(value = "execution(* AspectJ.anno.User.add(..))")    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {        System.out.println("环绕之前.........");        proceedingJoinPoint.proceed();        System.out.println("环绕之后.........");    }}

          2. 其他类型的通知

          package AspectJ.anno;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.springframework.stereotype.Component;/** * 增强的类 */@Component@Aspectpublic class UserProxy {    @Before(value = "execution(* AspectJ.anno.User.add(..))")    public void before() {        System.out.println("before......");    }    @After(value = "execution(* AspectJ.anno.User.add(..))")    public void after() {        System.out.println("after.........");    }    @AfterReturning(value = "execution(* AspectJ.anno.User.add(..))")    public void afterReturning() {        System.out.println("afterReturning.........");    }    @AfterThrowing(value = "execution(* AspectJ.anno.User.add(..))")    public void afterThrowing() {        System.out.println("afterThrowing.........");    }}

          简单学习

          切入点表达式

        3. 作用:明确对哪个类里面的哪个方法进行增强。
        4. 语法结构execution( [权限修饰符] [返回类型] [类全路径] [方法名称]([参数列表]) )
        5. 示例

        6. com.atguigu.dao.BookDao 类里面的 add 方法进行增强:
          execution(* com.atguigu.dao.BookDao.add(..))
        7. com.atguigu.dao.BookDao 类里面的所有方法进行增强:
          execution(* com.atguigu.dao.BookDao.* (..))
        8. com.atguigu.dao 包里面所有类,类里面所有方法进行增强:
          execution(* com.atguigu.dao.*.* (..))
        9. 多个增强类同一个方法进行增强,设置增强类优先级

          示例

          package AspectJ.anno;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;/** * 增强的类 */@Component@Aspect@Order(value = 1)public class StudentProxy {    @Pointcut(value = "execution(* AspectJ.anno.User.add(..))")    public void point() {}    @Before(value = "point()")    public void before() {        System.out.println("Student Before ,........");    }    @After(value = "point()")    public void after() {        System.out.println("Student After.........");    }    @AfterReturning(value = "point()")    public void afterReturning() {        System.out.println("Student afterReturning.........");    }}

          基于 XML 配置文件方式操作实现 AOP

          第一步:创建被增强的类和增强类

          package AspectJ.xml;public class People {    public void eating() {        System.out.println("eating......");    }}package AspectJ.xml;public class PeopleProxy {    public void before() {        System.out.println("before.......");    }}

          第二步:在 Spring 配置文件中创建两个类对象

          第三步:在 Spring 配置文件中共配置切入点

          第四步:测试

          public class Test {    @Test    public void testXml() {        try {            ApplicationContext context = new ClassPathXmlApplicationContext("AspectJ/bean_xml.xml");            People people = context.getBean("people", People.class);            people.eating();        } catch (Exception e) {            e.printStackTrace();        }    }}

          通过以上配置和测试,可以清晰地看到 AspectJ 在基于 XML 配置文件的方式下,成功实现了 AOP 操作。

    转载地址:http://cbeq.baihongyu.com/

    你可能感兴趣的文章
    NOIP2014 提高组 Day2——寻找道路
    查看>>
    noip借教室 题解
    查看>>
    NOIP模拟测试19
    查看>>
    NOIp模拟赛二十九
    查看>>
    Nokia5233手机和我装的几个symbian V5手机软件
    查看>>
    Non-final field ‘code‘ in enum StateEnum‘
    查看>>
    none 和 host 网络的适用场景 - 每天5分钟玩转 Docker 容器技术(31)
    查看>>
    None还可以是函数定义可选参数的一个默认值,设置成默认值时实参在调用该函数时可以不输入与None绑定的元素...
    查看>>
    NOPI读取Excel
    查看>>
    NoSQL&MongoDB
    查看>>
    NoSQL介绍
    查看>>
    Notadd —— 基于 nest.js 的微服务开发框架
    查看>>
    Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Notepad++在线和离线安装JSON格式化插件
    查看>>
    notepad++最详情汇总
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>