Spring注解开发——27、AOP-AOP功能测试

27、AOP-AOP功能测试

AOP : 【动态代理】指程序运行期间动态的将某段代码切入到制定方法位置进行运行的编程方式。


  1. 导入AOP模块:Spring AOP(spring-aspects)
  2. 定义一个业务逻辑类(Mathcalculator);在业务逻辑运行的时候将日志进行打印(方法之前、方法运行之后等)
  3. 定义一个日志切面类(LogAspects):切面类里面的方法需要动态感知MathCalculator(div)运行到哪里,然后执行
  4. 方法通知:
    1. 前置通知(@Befor):LogStart
    2. 后置通知(@After):LogEnd
    3. 返回通知(@AfterReturning):LogReturn
    4. 异常通知(@AfterThrowing):LogException
    5. 环绕通知(@Aound):动态代理,手动推进目标方法运行(joinPoint.procced())
  5. 将切面类的目标犯法标注何时何地运行(通知注释);
  6. 将切面类和业务逻辑类(目标方法所在类 )加入到容器类;
  7. 配置类中@EnableAspectJAutoProxy 开启基于注解的AOP模式
        
            org.springframework
            spring-aspects
            5.0.5.RELEASE
        

27.1 实例

1、业务逻辑

package com.suirui.springanno.aop.service;

import org.springframework.stereotype.Component;

@Component
public class MathCalculator {

    public int div(int i, int j) {
        System.out.println("MathCalculator   div 运行...");
        return i / j;
    }
}

2、@Configuration,配置文件

package com.suirui.springanno.aop.conifg;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan(value = "com.suirui.springanno.aop.*")
@EnableAspectJAutoProxy
public class MainConfigAop {
}

3、切面类

package com.suirui.springanno.aop.aop;

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

import java.util.Arrays;

@Aspect
@Component
public class LogAspects {


    @Pointcut("execution(public int com.suirui.springanno.aop.service.MathCalculator.*(..))")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName(); //方法名
        Object[] args = joinPoint.getArgs();    //参数列表
        System.out.println(methodName + "运行,参数列表是:{" + Arrays.asList(args) + "}");
    }

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


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


    @AfterThrowing( value = "pointCut()", throwing = "exception")
    public void logException(JoinPoint joinPoint,Exception exception) {
        System.out.println(joinPoint.getSignature().getName() + "除法运行异常,异常信息:" + exception.getMessage());
    }

}

4、测试

package com.suirui.springanno.aop;

import com.suirui.springanno.aop.conifg.MainConfigAop;
import com.suirui.springanno.aop.service.MathCalculator;
import com.suirui.springanno.register.config.MainConfig2;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainTestAop {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MainConfigAop.class);
        //获取所有的BeanDefinitionNames
//        String[] beanNames = ac.getBeanDefinitionNames();
//        for (String s : beanNames) {
//            System.out.println(s);
//        }

        MathCalculator mathCalculator =(MathCalculator) ac.getBean("mathCalculator");
        mathCalculator.div(1,0);
    }
}

Spring注解开发——27、AOP-AOP功能测试_第1张图片

你可能感兴趣的:(Spring注解开发)