【技术】Spring Aop 实例( 简单日志框架)

Spring Aop 实例( 简单日志框架)

近期翻看 Spring Aop 讲义时发现 Aop 实例这一章节只给了了了几个字,并没有实际的案例。于是就编写了简单的日志框架,进一步了解了 Aop 的作用。功能虽然简单,但确实有助于理解 Aop 的设计理念,面向切面编程。
持久层(dao)仅仅编写核心代码,通过 Aop 实现日志,在控制台展示代码的执行流程。

增强类:

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * 实现日志的需求
 * 1、方法的执行开始
 * 2、方法的执行结束
 * 3、方法的执行结果
 * 4、方法的执行参数
 * 5、方法的执行异常
 */
@Component
@Aspect
@Order(0)
public class LogAspect {
    @Around("execution(* com.soft.service.*.*(..))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // 日期格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        // 核心的业务对象(方法)
        Object target = proceedingJoinPoint.getTarget();
        // 截取字符串去掉地址符
        String s = target.toString();
        int index = s.lastIndexOf("@");
        String substring = s.substring(0, index);

        System.out.println(red("[日志] " + format + "\t" + substring + " 开始执行...."));
        // 方法执行的参数
        Object[] args = proceedingJoinPoint.getArgs();
        // 借助数组的工具类
        System.out.println(red("[日志] " + format + "\t" + "参数:" + Arrays.toString(args)));
        // 方法执行的结果
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println(red("[日志] " + format + "\t" + substring + " 结束执行,结果:" + proceed));
        return proceed;
    }

    @AfterThrowing(value = "execution(* com.soft.service.*.*(..))", throwing = "e")
    public void afterThrowing(Exception e){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        System.out.println(red("[日志] " + format + "\t" + "异常:" + e));
    }

    public String red(String old){
        return "\033[1;31m" + old + "\033[0m";
    }
}

配置文件:

<?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
        https://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.soft"/>

    <!-- 使AOP的注解起作用 -->
    <aop:aspectj-autoproxy/>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值