Spring 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()")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值