spring boot:AOP

2019-11-20  本文已影响0人  远方的橄榄树

1、简介

2、AOP中的概念

3、AOP的通知类型

4、快速入门

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
public class Calculator {

    public int divide(int a, int b) {
        System.out.println("除法:" + a + "/" + b);
        return a / b;
    }

    public int add(int a, int b) {
        System.out.println("加法:" + a + "+" + b);
        return a + b;
    }
}
@Aspect
public class CalculatorAop {

    //切点 Calculator的+-*/方法
    @Pointcut("execution(public int com.samllbear.aopdemo.service.Calculator.divide(..))")
    public void pointCut() {}

    @Before("pointCut()")
    public void before(JoinPoint joinPoint) {
        System.out.println("Before...");
        Signature signature = joinPoint.getSignature(); // 获取方法的签名
        System.out.println("执行运算:" + signature.getName()); // 获取方法名称
        System.out.println("执行参数:" + Arrays.toString(joinPoint.getArgs())); // 方法参数
    }

    @After("pointCut()")
    public void after() {
        System.out.println("After...");
    }

    @AfterReturning(value = "pointCut()", returning = "result")
    public void afterReturning(Object result) {
        System.out.println("Returning...");
        System.out.println("执行结果:" + result);
    }

    @AfterThrowing(value = "pointCut()", throwing = "exception")
    public void afterThrowing(Exception exception) {
        System.out.println("Throwing...");
        System.out.println(exception.getMessage());
    }
}
@Configuration
@EnableAspectJAutoProxy // 开启aop
public class CalculatorConfig {

    @Bean // 注入计算器
    public Calculator calculator() {
        return new Calculator();
    }

    @Bean // 注入aop
    public CalculatorAop calculatorAop() {
        return new CalculatorAop();
    }
}
class AopDemoApplicationTests {

    @Test
    public void divideTest() {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(CalculatorConfig.class);
        // 注意只有注入容器的bean AOP才会生效
        Calculator calculator = context.getBean(Calculator.class);
//        calculator.divide(13, 0);  // throwing... /By Zero
        calculator.divide(13, 2);
    }
}
Before...
执行运算:divide
执行参数:[13, 2]
除法:13/2
After...
Returning...
执行结果:6
    private static final Logger log = LoggerFactory.getLogger(CalculatorAop.class);

    @Pointcut("execution(public int com.samllbear.aopdemo.service.Calculator.add(..))")
    public void addPointCut() {}

    @Around("addPointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        try {
            System.out.println("around begin...");
            Object o = joinPoint.proceed(); // 执行目标方法
            System.out.println("around end...");
            return o;
        } catch (Throwable e) {
            System.out.println("around throwing...");
            log.error(e.getMessage());
            throw e;
        } finally {
            System.out.println("around returning...");
        }
    }
    @Test
    public void addTest() {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(CalculatorConfig.class);
        Calculator calculator = context.getBean(Calculator.class);
        calculator.add(13, 0);
    }
around begin...
加法:13+0
around end...
around returning...
上一篇下一篇

猜你喜欢

热点阅读