AOP基于注解的日志通知

AOP前置通知 我们需要的用到的注解参数及ioc容器中需要进行的配置

注解参数:① @Component控制器(注入服务)② @Aspect声明一个切面③ @Before

ioc容器的配置:   <context:component-scan base-package="  "></context:component-scan>
                                 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

给通知加入一些细节(参数,方法名)

    @Before("execution(public int com.qst.aop.ArithmeticCalculator.add(int, int))") //红色标记可用 * 代替 代表任意(权限 类型 类)
    public void befordlog(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();//获得方法名
        List<Object> args = Arrays.asList(joinPoint.getArgs());//获得方法参数
        System.out.println("方法"+methodName+"with"+args);
    }

代码截图

AOP后置通知与前置通知大体一致,只不过把@before换为@After (无论出现异常都会执行)

除此之外,aop还给我吗提供了 @AfterReturning、 @AfterThrowing、@Around来指定配置,不同切面通过index( 数字 )来指定优先级。

通过@Pointcut声明切入点表达式:
    @Pointcut("execution(public int com.qst.aop.log.ArithmeticCalculator.*(..))")
    public void point() {} 

以后直接调用point即可 如@After("point()")

猜你喜欢

转载自blog.csdn.net/qq_42183409/article/details/89285217
今日推荐