AOP注解的使用

文章目录

AOP的使用

AOP指的是程序运行期间动态的将某段代码切入到指定位置进行运行的编程。

  1. 导入AOP模块:Spring AOP:spring-aspects
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
  1. 创建一个业务逻辑类,在业务逻辑运行的时候将日志进行打印(方法运行之前、方法运行之后、方法运行出现异常等)
package com.atguigu.spring_annotation.aop;

public class MathCalculator {
    public int div(int i, int j) {
        System.out.println("MathCalculator...div");
        return i / j;
    }
}
  1. 创建一个切面类,负责打印业务逻辑类的日志信息,切面类需要加上@Aspect注解
  • 通知方法(@Before):前置通知:logStart:在目标方法(div)运行之前运行
  • 后置通知( @After):logEnd:在目标方法(div)运行结束之后运
  • 返回通知(@AfterReturning):logReturn:在目标方法(div)正常返回之
  • 运行异常通知(@AfterThrowing):logException:在目标方法(div)出现异常以后运行
  • 环绕通知(@Around):动态代理,手动推进目标方法运行(JoinPoint.proceed)
package com.atguigu.spring_annotation.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

@Aspect
public class LogAspects {
    //本类引用 "pointCut()"
    //其他的切面引用 “com.atguigu.spring_annotation.aop.LogAspects.pointCut()”
    @Pointcut("execution(public int com.atguigu.spring_annotation.aop.MathCalculator.*(..))")
    public void pointCut() {
    }

    //在切入点表达式指定的目标方法前切入
    @Before("execution(public int com.atguigu.spring_annotation.aop.MathCalculator.div(int, int))")
    public void logStart(JoinPoint joinPoint) {
        System.out.println(joinPoint.getSignature().getName() + "运行@Before...参数是:{" + Arrays.toString(joinPoint.getArgs()) + "}");
    }

    @After("pointCut()")
    public void logEnd(JoinPoint joinPoint) {
        System.out.println(joinPoint.getSignature().getName() + "结束... @After");
    }

    @AfterReturning(value = "pointCut()", returning = "result")
    public void logReturn(JoinPoint joinPoint, Object result) {
        System.out.println(joinPoint.getSignature().getName() + "正常返回 @AfterReturning...运行结果:{" + result + "}");
    }

    @AfterThrowing(value = "pointCut()", throwing = "exception")
    public void logException(JoinPoint joinPoint, Exception exception) {
        System.out.println(joinPoint.getSignature().getName() + "异常@AfterThrowing...异常信息:{" + exception + "}");
    }
}
  1. 将切面类与被切面类加入到容器中,配置类必须加上@EnableAspectJAutoProxy注解用来开启基于注解的aop模式
package com.atguigu.spring_annotation.config;

import com.atguigu.spring_annotation.aop.LogAspects;
import com.atguigu.spring_annotation.aop.MathCalculator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@EnableAspectJAutoProxy
@Configuration
public class MainConfigOfAOP {
    @Bean
    public MathCalculator mathCalculator() {
        return new MathCalculator();
    }

    @Bean
    public LogAspects logAspects() {
        return new LogAspects();
    }
}
  1. 测试
package com.atguigu.spring_annotation.test;

import com.atguigu.spring_annotation.aop.MathCalculator;
import com.atguigu.spring_annotation.config.MainConfigOfAOP;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestAop {
    @Test
    public void test(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAOP.class);
        MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class);
        mathCalculator.div(12, 2);
        /*
        div运行@Before...参数是:{[12, 0]}
        MathCalculator...div
        div结束... @After
        div异常@AfterThrowing...异常信息:{java.lang.ArithmeticException: / by zero}
         */
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值