Aop面向切面编程

本文介绍了Spring框架中的AOP(面向切面编程)及其核心概念,包括动态代理、通知、切入点、切面和JoinPoint。通过示例展示了如何使用@Aspect、@Around通知以及切入点表达式来记录方法执行时间并获取相关参数。
摘要由CSDN通过智能技术生成

其实就是面向特定方法编程

动态代理是面向切面编程最主流的实现,而SpringAOP是Spring框架的高级技术,旨在管理bean对象的过程中,主要通过底层的动态代理机制,对特定的方法进行编程

导入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

入门程序

@Slf4j
@Component
@Aspect//AOP类的标志
public class TimeAspect {
    
    @Around("execution(* com.itheima.service.*.*(..))")//切入点表达式
    public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
        //1.记录开始时间
        long begin=System.currentTimeMillis();
        //2.调用原始方法运行
        Object result=joinPoint.proceed();
        //3.记录结束时间,计算方法执行耗时
        long end=System.currentTimeMillis();
        log.info(joinPoint.getSignature()+"方法执行时间:{}ms",end-begin);
        return result;
    }
}

连接点:joinPoint,可以被AOP控制的方法(暗含方法执行时的 相关信息)

通知:Advice,指那些重复的逻辑,也就是共性的功能(最终体现为一个方法)

切入点:PointCut,匹配练节点的条件,通知仅仅会在切入点方法执行时被应用

切面:Aspect,描述通知与切入点的对应关系(通知+切入点)

目标对象:Target,通知所应用的对象

通知类型

    @Pointcut("execution(* com.itheima.service.*.*(..))")
    public void pt(){}
    
    @Around("pt()")//切入点表达式

通知顺序:

当多个切面的切入点都匹配到了目标方法,目标方法运行时,多个通知方法都会被执行

切入点表达式

@annotation

    @Around("@annotation(com.itheima.anno.Log)")
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Log {
}

连接点

在Spring中用JoinPoint抽象了连接点,用它可以获得方法执行时的相关信息,如目标类名,方法名,方法参数等。

对于@Around通知,获取连接点信息只能使用ProceedingJoinPoint

对于其他四种通知,获取连接点信息只能使用JoinPoint,它是 ProceedingJoinPoint的父类型

@Slf4j
@Component
@Aspect//AOP类的标志
public class TimeAspect {

    @Pointcut("execution(* com.itheima.service.*.*(..))")
    public void pt(){}

    @Around("pt()")//切入点表达式
    public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取 目标对象的类名
        String className = joinPoint.getTarget().getClass().getName();

        //获取 目标方法的方法名
        String methodName = joinPoint.getSignature().getName();


        //获取 目标方法运行时传入的参数
        Object[] args = joinPoint.getArgs();

        //放行目标方法执行
        Object result = joinPoint.proceed();

        //获取 目标方法运行的返回值
        log.info("目标方法运行返回值:{}",result);
        return null;
    }
}

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的AOP面向切面编程的测试代码示例,使用Spring框架实现: 首先,创建一个切面类 `LoggingAspect`,用于定义切面逻辑: ```java 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; @Aspect @Component public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void beforeAdvice(JoinPoint joinPoint) { System.out.println("Before method: " + joinPoint.getSignature().getName()); } @After("execution(* com.example.service.*.*(..))") public void afterAdvice(JoinPoint joinPoint) { System.out.println("After method: " + joinPoint.getSignature().getName()); } } ``` 然后,创建一个测试服务类 `UserService`,用于演示AOP的应用: ```java import org.springframework.stereotype.Service; @Service public class UserService { public void createUser(String username) { System.out.println("Creating user: " + username); } public void deleteUser(String username) { System.out.println("Deleting user: " + username); } } ``` 最后,创建一个Spring Boot应用程序,并在启动类中进行配置: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.EnableAspectJAutoProxy; @SpringBootApplication @EnableAspectJAutoProxy public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); UserService userService = context.getBean(UserService.class); userService.createUser("John"); userService.deleteUser("John"); } } ``` 在上述示例中,`LoggingAspect` 切面类使用 `@Before` 和 `@After` 注解分别定义了在目标方法执行前和执行后的逻辑。切面逻辑会应用于 `UserService` 类中的所有方法。 当运行应用程序时,可以看到切面逻辑在方法执行前和执行后打印了相应的日志消息。 这是一个简单的AOP面向切面编程的示例,你可以根据实际需求进行更复杂的切面逻辑定义和应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值