spring aop记录操作日志和错误日志

AOP介绍

aop这个概念不陌生了,我们就结合下边这个图来简单的介绍一下aop中的几个概念。
这里写图片描述
AOP:Aspect-Oriented Programming的缩写
JoinPoint:要切入的点,例如我们图中的addUser方法就是一个JoinPoint。
Pointcut:定系统中符合条件的一组Joinpoint。
Aspect:就是我们的切面,例如我们的日志类,里边包含了记录操作日志,记录异常日志等,那么这个对象就是一个切面。
Advice:怎么切,是在业务逻辑执行前去执行还是之后执行等。

aop实现

说了这么多,我们就以上边的图和名词解释来结合代码来讲一下他们的含义和如何执行的。

环境

这里写图片描述

业务类

一个类实现了一个接口addUser

public interface UserManager {
    public void addUser(String username,String password);
}
public class UserManagerImpl implements UserManager {

    @Override
    public void addUser(String username, String password) {
        int a = 1/0;
        System.out.println("addUser");

    }
}

注意:这里的addUser就是我们的JoinPoint,也就是切点,我们要切进去的方法。

切面(Aspect)

/**
 * 定义aspect,将横切点模块化
 * 
 * @author mengh
 * 
 */

public class SecurityHandler {

    /**
     * 操作日志
     * 
     * @param joinPoint
     */
    public void checkSecurity(JoinPoint joinPoint) {
        System.out.println(joinPoint.getTarget().getClass() + "类"
                + joinPoint.getSignature().getName());

    }

    /**
     * 异常日志
     * 
     * @param joinPoint
     */

    public void doThrowing(JoinPoint jp, Throwable ex) {
        System.out.println(jp.getTarget().getClass() + "类的方法的" + jp.getSignature().getName() + "可以记录程序运行时候抛出的异常信息: " + ex.getMessage());
    }

}

注意:这里的SecurityHandler 就是我们的切面,里边包含了我们要切入的操作。
方法的参数中JoinPoint 包含了我们切入方法的各个属性,包括类,方法名,字段名等等的信息。

applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <!-- 启用acpect对注解的支持 -->
    <!-- <aop:aspectj-autoproxy></aop:aspectj-autoproxy> -->
    <bean id="userManager" class="com.tgb.spring.UserManagerImpl" />
    <bean id="securityHandler" class="com.tgb.spring.SecurityHandler" />
    <aop:config>
        <aop:aspect id="securityAspect" ref="securityHandler">
            <!-- <aop:pointcut id="addAddMethod" expression="execution(* add*(..))"/> -->

            <!-- 操作日志的处理 -->
            <aop:pointcut id="addAddMethod" expression="execution(* com.tgb.spring.*.add*(..)) ||execution(* com.tgb.spring.*.del*(..))"/>
            <aop:before method="checkSecurity" pointcut-ref="addAddMethod"/>
            <aop:after method="checkSecurity" pointcut-ref="addAddMethod"/>

            <!-- 异常日志的处理 -->
            <!-- <aop:pointcut id="addExceptionMethod" expression="execution(* com.tgb.spring.*.*(..)) || args(name)"/> --> 
             <aop:after-throwing method="doThrowing" pointcut="execution(* com.tgb.spring.*.add*(..))" throwing="ex"/> 

        </aop:aspect>
    </aop:config>
</beans>

注意:代码中aop:before等就是我们说的advice,大家按照字面的意思,就是告诉spring我要怎么切,是在方法执行之前就执行还是之后等等。
aop:pointcut就是告诉spring我们要切入的规则,例如这里边就是所有添加的方法都要执行操作日志的。
method:就是对应我们的切面类的方法名称。

客户端

public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserManager userManager = (UserManager)beanFactory.getBean("userManager");
        userManager.addUser("zhangssan", "wangwu");

    }

结果

这里写图片描述
红框是我们的操作类,告诉在什么时候操作了什么类的什么方法,个人感觉这个中类实际意就是测试性能的时候大一些,但是我们蓝框中的错误处理,这样我们的程序出错了,我们也能很清晰的找到我们的程序是哪里出错了,加上时间的话,就能找到出错的时间。
但是如果我们不用呢,是不是我们的程序如果需要加一个日志的方法,就要在所有的类里边,加上错误处理,加上操作日志,这样的话,如果我们要删除这个操作日志,觉得没什么用,那么我们就要一点一点的的去删除了。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Spring AOP是一个强大的框架,可以帮助我们实现各种切面,其中包括日志记录。下面是实现日志记录的步骤: 1. 添加Spring AOP依赖 在Maven或Gradle中添加Spring AOP依赖。 2. 创建日志切面 创建一个用于记录日志的切面。这个切面可以拦截所有需要记录日志的方法。在这个切面中,我们需要使用@Aspect注解来声明这是一个切面,并使用@Pointcut注解来定义哪些方法需要被拦截。 ```java @Aspect @Component public class LoggingAspect { @Pointcut("execution(* com.example.demo.service.*.*(..))") public void serviceMethods() {} @Around("serviceMethods()") public Object logServiceMethods(ProceedingJoinPoint joinPoint) throws Throwable { // 获取方法名,参数列表等信息 String methodName = joinPoint.getSignature().getName(); Object[] args = joinPoint.getArgs(); // 记录日志 System.out.println("Method " + methodName + " is called with args " + Arrays.toString(args)); // 执行方法 Object result = joinPoint.proceed(); // 记录返回值 System.out.println("Method " + methodName + " returns " + result); return result; } } ``` 在上面的代码中,我们使用了@Around注解来定义一个环绕通知,它会在拦截的方法执行前后执行。在方法执行前,我们记录了该方法的名称和参数列表,然后在方法执行后记录了该方法的返回值。 3. 配置AOPSpring的配置文件中配置AOP。首先,我们需要启用AOP: ```xml <aop:aspectj-autoproxy/> ``` 然后,我们需要将创建的日志切面添加到AOP中: ```xml <bean id="loggingAspect" class="com.example.demo.aspect.LoggingAspect"/> <aop:config> <aop:aspect ref="loggingAspect"> <aop:pointcut id="serviceMethods" expression="execution(* com.example.demo.service.*.*(..))"/> <aop:around method="logServiceMethods" pointcut-ref="serviceMethods"/> </aop:aspect> </aop:config> ``` 在上面的代码中,我们将创建的日志切面声明为一个bean,并将其添加到AOP中。我们还定义了一个切入点,并将其与日志切面的方法进行关联。 4. 测试 现在,我们可以测试我们的日志记录功能了。在我们的业务逻辑中,所有匹配切入点的方法都会被拦截,并记录它们的输入和输出。我们可以在控制台中看到这些日志信息。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值