spring boot aop demo

本文详细介绍了SpringBoot中AOP的实现方式,包括切面、切点和通知的概念,通过具体示例展示了如何使用SpringBoot集成AOP进行方法前后处理,以实现日志记录等通用功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring aop demo

spring aop

spring aop是一项面向切面编程的技术,根据某个规则将与核心业务无关的代码解耦,使开发只关注核心代码。spring 采用动态代理技术实现了aop。

面向切面概念

切点:触发通知的方法即触发规则
通知:触发规则后应该执行什么操作
切点和通知构成了一个切面类

spring boot aop demo 源码

切面类
package com.freedom.aspect;

import java.lang.reflect.Modifier;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * @author :  freedom
 * @Description :  aop 切面配置类 Aspect: 标明这是一个切面配置类 Component: 由IOC管理实例化此类
 * @Creation Date:  2019-12-31 4:50 下午
 */
@Aspect
@Component
public class AopAspect {

  /**
   * 设置在方法执行之前的通知类型
   * value中的execution是切点规则
   * beforeLog是通知方法
   * 通知和切点构成了切面
   * JoinPoint链接点对象,包括目标方法的相关信息,以及代理的相关信息
   */
  @Before(value = "execution(* com.freedom..*.*(..))")
  public void beforeLog(JoinPoint joinPoint) {
    /**
     * 打印JoinPoint的相关信息
     */
    System.out.println( "目标方法名为:" + joinPoint.getSignature().getName() );
    System.out
        .println( "目标方法所属类的简单类名:" + joinPoint.getSignature().getDeclaringType().getSimpleName() );
    System.out.println( "目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName() );
    System.out
        .println( "目标方法声明类型:" + Modifier.toString( joinPoint.getSignature().getModifiers() ) );
    //获取传入目标方法的参数
    Object[] args = joinPoint.getArgs();
    for (int i = 0; i < args.length; i++) {
      System.out.println( "第" + (i + 1) + "个参数为:" + args[i] );
    }
    System.out.println( "被代理的对象:" + joinPoint.getTarget() );
    System.out.println( "代理对象自己:" + joinPoint.getThis() );
    /**
     * 方法执行前执行的内容
     */
    System.out.println( "在方法执行前 执行" );
  }

  /**
   * 设置在方法执行之后的通知类型
   * value中的execution是切点规则
   * afterLog是通知方法
   * 通知和切点构成了切面
   * JoinPoint链接点对象,包括目标方法的相关信息,以及代理的相关信息
   */
  @After(value = "execution(* com.freedom..*.*(..))")
  public void afterLog(JoinPoint joinPoint) {
    /**
     * 方法执行后执行的方法
     */
    System.out.println( "在方法执行后 执行" );
  }
}

切点方法类
package com.freedom.service;

import org.springframework.stereotype.Service;

/**
 * @author :  freedom
 * @Description :  service 服务类
 * @Creation Date:  2019-12-31 2:10 下午
 */
@Service
public class AopService {

  /**
   * 添加一个用户
   */
  public void add(String name, String password) {
    System.out.println( "添加一个用户,用户名:" + name + ",密码:" + password );
  }

  /**
   * 删除一个用户
   */
  public void delete() {
    System.out.println( "删除一个用户" );
  }

  /**
   * 查询一个用户
   */
  public void query() {
    System.out.println( "查询一个用户" );
  }

  /**
   * 修改一个用户
   */
  public void modify() {
    System.out.println( "修改一个用户" );
  }
}

spring boot 测试类
package com.freedom.aop;

import com.freedom.service.AopService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * spring boot 测试类
 */
@SpringBootTest
class AopApplicationTests {

  /**
   * aopService实例由IOC容器注入
   */
  @Autowired
  private AopService aopService;

  /**
   * 测试方法
   */
  @Test
  void contextLoads() {
    /**
     * 调用添加用户方法
     */
    aopService.add( "test", "test1234" );
  }

}
spring boot 集成aop
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
      <version>2.2.2.RELEASE</version>
    </dependency>

spring boot 集成aop 只需要添加依赖即可,spring boot 已默认开启aop功能

运行测试用例结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xh7eynA0-1577942703870)(evernotecid://AAA42DFC-EC45-4768-B516-25846DA75603/appyinxiangcom/12441645/ENResource/p2133)]

参考链接

参考链接

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bigdatafreedom

你的鼓励奖是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值