利用Spring的AOP实现日志打印

mac2025-11-26  13

要求: 使用AOP实现计算器功能增强,调用方法时,在对应位置添加日志

步骤: 1.导包:要使用@Aspect、@Before、@AfterReturning、@AfterThrowing等注解,需要导入 aspect包 2.将目标类和切面类放入ioc容器:添加对应的注解,切面类要添加@Aspect,告知容器他是切片类 3.给切面类里面的方法添加注解,告知个方法何时何地切入。 4.在配置文件中开启aop:≶aop:aspectj-autoproxy><aop:aspectj-autoproxy>

切点表达式:

一般形式:execution(访问全限修饰符 返回类型 方法全类名(参数)) 通配符: *: 1.匹配一个或多个字符 2.匹配任意类型 . . : 1.匹配任意个,任意类型参数 2.任意多层路径

参考代码:

<?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 http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <context:component-scan base-package="com.gang"></context:component-scan> <!-- 开启aop --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans> package com.gang.inter; public interface Calculator { public int add(int i,int j); public int sub(int i,int j); public int mul(int i,int j); public int div(int i,int j); } package com.gang.impl; import org.springframework.stereotype.Service; import com.gang.inter.Calculator; @Service public class MyCalculator implements Calculator { @Override public int add(int i, int j) { //手动为方法添加日志 //LogUtils.startLog(i,j,"加法"); int result; result=i+j; return result; } @Override public int sub(int i, int j) { int result; result=i-j; return result; } @Override public int mul(int i, int j) { int result; result=i*j; return result; } @Override public int div(int i, int j) { int result; result=i/j; return result; } } package com.gang.utils; import java.util.Arrays; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class LogUtils { @Before("execution(public int com.gang.impl.MyCalculator.*(int, int))") public static void startLog(JoinPoint joinPoint){ Object[] args=joinPoint.getArgs(); Signature signature = joinPoint.getSignature(); String methodName=signature.getName(); System.out.println("【"+methodName+"】执行开始了,他的参数为【"+Arrays.asList(args)+"】"); } @AfterReturning(value="execution(public int com.gang.impl.MyCalculator.*(int, int))",returning="result") public static void endLog(JoinPoint joinPoint,Object result){ System.out.println("【"+joinPoint.getSignature().getName()+"】执行结束了,他的结果为【"+result+"】"); } @AfterThrowing(value="execution(public int com.gang.impl.MyCalculator.*(int, int))",throwing="e") public static void exceptionLog(JoinPoint joinPoint,Exception e){ System.out.println("【"+joinPoint.getSignature().getName()+"】执行出错了,错误信息为【"+e+"】"); } @After("execution(public int com.gang.impl.MyCalculator.*(int, int))") public static void finallyLog(JoinPoint joinPoint){ System.out.println("【"+joinPoint.getSignature().getName()+"】最终执行结束了"); } } package com.gang.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.gang.impl.MyCalculator; public class CalculatorTest { ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml"); @Test public void test(){ MyCalculator c=ac.getBean(MyCalculator.class); c.add(8, 2); System.out.println("-------------------------------"); c.div(8, 0); } }

运行结果:

注意: 通过接口方式实现的aop实质是动态代理,在ioc容器中实际存放的类型是包装类型,所以从ioc容器中的获得时应返回接口类型,因为返回对象实质是包装类。

执行顺序为 : 正常:@Before==》@After==》@AfterReturning 异常:@Before==》@After==》@AfterThrowing

最新回复(0)