基于 AspectJ 注解方式实现AOP(1)

AOP 操作(准备工作)

1、Spring 框架一般都是基于 AspectJ 实现 AOP操作

(1)AspectJ 不是 Spring 组成部分,独立 AOP框架,一般把 AspectJ 和 Spring 框架一起使用,进行 AOP 操作

2、基于 AspectJ 实现 AOP 操作,有两种方式

(1)基于 xml 配置文件实现

(2)基于注解方式实现(推荐使用,本文说的就是注解方式实现 AOP)

3、在项目里面引入 AOP 相关依赖

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.5.4</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.9.1</version>
        </dependency>

4、切入点表达式

(1)切入点表达式作用:知道对哪个类里面的哪个方法进行增强

(2)语法结构:

execution([权限修饰符][返回类型][类全路径][方法名称]([参数列表]))

举例1:对 om.lmgd.server.impl.UserServer 类里面的 add() 方法 增强

execution(* com.lmgd.server.impl.UserServer.add(..))

举例2:对 com.lmgd.server.impl 里面的所有类,类里面所有方法增强

execution(* com.lmgd.server.impl.*(..))

AOP 操作(AspectJ 注解)

1、创建类,在类里面定义方法 

/**
 * 被增强的类
 *
 * @author 邓林妹
 * @date 2022/4/30
 */
public class User {

    public void add() {
        System.out.println("add......");
    }
}

2、创建增强类(编写增强逻辑) 

(1)在增强类里面,创建方法,让不同方法代表不同通知类型。 

/**
 * 增强的类
 */
public class UserProxy {
    /**
     * 前置通知
     */
    public void before() {
        System.out.println("before......");
    }
}

 3、进行通知的配置

(1)在 Spring 配置文件中,开启注解扫描

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--开启注解扫描-->
    <context:component-scan base-package="com.lmgd.aop"></context:component-scan>
</beans>

(2)使用注解创建 User 和 UserProxy 对象

/**
 * 被增强的类
 */
@Component
public class User {}
/**
 * 增强的类
 */
@Aspect //加上 @Aspect 注解:代表生成代理对象
@Component
public class UserProxy {}

(3)在增强类上面添加注解 @Aspect

/**
 * 增强的类
 */
@Aspect //加上 @Aspect 注解:代表生成代理对象
@Component
public class UserProxy {}

(4)在 Spring 配置文件中开启生成代理对象

<!--开启 Aspect 生成代理对象-->
<!--开启Aspect注解后, 到SPring IOC容器去找,哪个类上有 @Aspect 注解,有注解生成代理对象。-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

4、配置不同类型的通知 

 (1)在增强类里面,在作为通知方法上面添加通知类型注解,使用切入点表达式配置。

/**
 * 增强的类
 */
@Aspect //加上 @Aspect 注解:代表生成代理对象
@Component
public class UserProxy {
    /**
     *  @Before注解: 表示作为前置通知
     */
    @Before(value = "execution(* com.lmgd.aop.User.add(..))")
    public void before() {
        System.out.println("before......");
    }
}
package com.lmgd.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 增强的类
 */
@Aspect //加上 @Aspect 注解:代表生成代理对象
@Component
public class UserProxy {
    /**
     * @Before注解: 表示作为前置通知
     */
    @Before(value = "execution(* com.lmgd.aop.User.add(..))")
    public void before() {
        System.out.println("before-前置通知......");
    }

    /**
     * 返回通知(又名:后置通知):但是此方法是在在返回值之后执行,且有异常就不会执行
     */
    @AfterReturning(value = "execution(* com.lmgd.aop.User.add(..))")
    public void afterReturning() {
        System.out.println("afterReturning......");
    }

    /**
     * After通知:在方法之后执行,不管有无异常都会执行
     */
    @After(value = "execution(* com.lmgd.aop.User.add(..))")
    public void after() {
        System.out.println("after-后置通知......");
    }

    /**
     * 异常通知:被增强的方法里面发生异常时就会执行
     */
    @AfterThrowing(value = "execution(* com.lmgd.aop.User.add(..))")
    public void afterThrowing() {
        System.out.println("afterThrowing......");
    }

    /**
     * 环绕通知:执行被增强方法之前之后都会执行
     */
    @Around(value = "execution(* com.lmgd.aop.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕之前......");

        //执行被增强的方法
        proceedingJoinPoint.proceed();

        System.out.println("环绕之后......");
    }
}

 5、测试通知类型效果

 (1)测试前置通知(执行 add() 方法前,先执行前置通知方法)

 效果:执行 add()方法前,先执行前置通知方法

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值