SpringAOP使用:为一些方法添加日志,实现方法动态切入,注解以及配置文件实现

目标方法

先创建一个类,用来实现简单的加减乘除操作。利用接口和实现层实现。

接口层
package com.xzy.intr;


public interface Calculator {
    int add(int i, int j);

    int sub(int i, int j);

    int mul(int i, int j);

    int div(int i, int j);

}
实现层
import com.xzy.intr.Calculator;
import org.springframework.stereotype.Service;

@Service
public class CalculatorImpl implements Calculator {

    public int add(int i, int j) {
        return i + j;
    }

    public int sub(int i, int j) {
        return i - j;
    }

    public int mul(int i, int j) {
        return i * j;
    }
    public int div(int i, int j) {
        return i / j;
    }
}

写配置

将所有的组件加入到ioc容器中

    <context:component-scan base-package="com.xzy"></context:component-scan>
切面类的编写和加入容器

切面类的某个方法何时运行

package com.xzy.utils;

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

import java.util.Arrays;

/**
 * 切面类,方法是通知方法
 */
@Aspect
@Component
public class LogUtils {
    /**
     * 切入点表达式的写法:
     * 固定格式:execution(访问修饰符  返回值类型  方法全签名 (参数类型))
     * 通配符:*
     * 匹配一个或多个字符
     * 匹配一个参数
     * 匹配一层路径
     * ..
     * 匹配任意类型的多个参数
     * 匹配任意多层路径
     * <p>
     * 还可以将多个表达式用&& ||  !  来连接进行逻辑判断(了解即可)
     * <p>
     * 通知方法的执行顺序
     * 正常执行
     * @Before 前置通知 --> @After后置通知 --> @AfterReturning 返回
     * <p>
     * 异常执行
     * @Before 前置通知 --> @After后置通知 --> @AfterThrowing  异常
     */

    //目标方法之前运行
    //要写切入点表达式
    @Before("execution(public int com.xzy.impl.CalculatorImpl.*(int,int))")
    public static void logStart(JoinPoint joinPoint) {
        //获取方法签名
        Signature signature = joinPoint.getSignature();
        //获取方法名
        String name = signature.getName();
        System.out.println("【" + name + "】方法开始执行,参数是:【" + Arrays.asList(joinPoint.getArgs()) + "】");
    }

    //目标方法返回时运行
    //returning指定那个参数接收返回值
    @AfterReturning(value = "execution(public int com.xzy.impl.CalculatorImpl.*(int,int))",
            returning ="result" )
    public static void logReturn(JoinPoint joinPoint,Object result) {
        System.out.println("【"+joinPoint.getSignature().getName()+"】方法正常返回,返回值是:【"+result+"】");
    }

    //目标方法出现异常运行
    //通过throwing指定那个参数接收异常
    @AfterThrowing(value = "execution(public int com.xzy.impl.CalculatorImpl.div(int,int))",
    throwing = "exception")
    public static void logException(JoinPoint joinPoint,Exception exception) {
        System.out.println("【"+joinPoint.getSignature().getName()+"】方法出现异常,异常信息是:【"+exception+"】");
    }

    //目标方法运行结束之后运行
    @After("execution(public int com.xzy.impl.CalculatorImpl.*(int,int))")
    public static void logEnd(JoinPoint joinPoint) {
        System.out.println("【"+joinPoint.getSignature().getName()+"】方法正常结束");
    }
}

开启基于注解的AOP模式

    <!--开启基于注解的功能 ,自动代理-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

这样就把通知方切入到目标方法中了。

【mul】方法开始执行,参数是:【[4, 5]】
【mul】方法正常结束
【mul】方法正常返回,返回值是:【20】
20
com.xzy.impl.CalculatorImpl@6a03bcb1
class com.sun.proxy.$Proxy19

Process finished with exit code 0

环绕通知

@Around:是spring中强大的通知,其实就是动态代理

环绕通知可以整合其他四个通知,而且通知方法(切面类的方法)的顺序和声明一致。

    @Around("execution(public int com.xzy.impl.CalculatorImpl.*(int,int))")
    public static Object LogAround(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        //利用反射调用目标方法
        Object proceed = null;
        try {
            //此处为环绕前置通知
            proceed = pjp.proceed(args);//目标方法
            //此处为环绕返回通知
        } catch (Throwable throwable) {
            //此处为环绕出现异常通知
        } finally {
            //环绕后置通知
        }
        System.out.println("环绕通知。。。。");
        return proceed;
    }
注解实现

注解和配置文件的切面类的配置只能选一种

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--包扫描-->
    <context:component-scan base-package="com.xzy"></context:component-scan>

    <!--开启基于注解的功能 ,自动代理-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <!--将组件加入到IOC容器中-->
    <bean id="calculator" class="com.xzy.impl.CalculatorImpl"/>
    <bean class="com.xzy.utils.LogUtils" id="logUtils2"/>

    <!--配置切面-->
    <aop:config>
        <aop:aspect ref="logUtils">
            <aop:pointcut id="pointCut" expression="execution(public int com.xzy.impl.CalculatorImpl.*(int,int))"/>
            <!--配置那个方法是前置通知-->
            <aop:before method="logStart" pointcut="execution(public int com.xzy.impl.CalculatorImpl.*(int,int))"/>
            <!--引用切入点表达式-->
            <aop:after method="logEnd" pointcut-ref="pointCut"/>
            <!--返回值和异常都是需要接受的-->
            <aop:after-returning method="logReturn" pointcut-ref="pointCut" returning="result"/>
            <aop:after-throwing method="logException" pointcut-ref="pointCut" throwing="exception"/>
            <!--其他配置是一样的-->
        </aop:aspect>
    </aop:config>

</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值