博客
关于我
【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/

    你可能感兴趣的文章
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>