一灰灰blog 一灰灰blog
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)

一灰灰blog

资深搬运工
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)
  • 基础系列

    • 配置

    • AOP

      • 【基础系列】AOP之基本使用姿势小结
      • 【基础系列】AOP之高级使用技能
      • 【基础系列】AOP之拦截优先级详解
      • 【基础系列】AOP实现一个日志插件(应用篇)
      • 【基础系列】接口上注解AOP拦截不到场景兼容
      • 【基础系列】Spring之AOP结合SpEL实现日志输出的注意事项
        • I. 项目环境
          • 1. 项目依赖
        • II. AOP & SpEL
          • 1. AOP切面
          • 2. StandardEvaluationContext安全问题
          • 3. gson序列化问题
          • 4. 小结
        • III. 不能错过的源码和相关知识点
          • 0. 项目
          • 1. 一灰灰Blog
    • Bean

    • SpEL

    • 事件

    • 国际化

    • 定时器

    • 日志

  • DB系列

  • 搜索系列

  • MQ系列

  • WEB系列

  • 中间件

  • 运维

  • SpringSecurity

  • SpringCloud

  • Spring系列
  • 基础系列
  • AOP
一灰灰
2021-06-08

【基础系列】Spring之AOP结合SpEL实现日志输出的注意事项

使用AOP来打印日志大家一把都很熟悉了,最近在使用的过程中,发现了几个有意思的问题,一个是SpEL的解析,一个是参数的JSON格式输出

# I. 项目环境

# 1. 项目依赖

本项目借助SpringBoot 2.2.1.RELEASE + maven 3.5.3 + IDEA进行开发

开一个web服务用于测试

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
1
2
3
4
5
6

# II. AOP & SpEL

关于AOP与SpEL的知识点,之前都有过专门的介绍,这里做一个聚合,一个非常简单的日志输出切面,在需要打印日志的方法上,添加注解@Log,这个注解中定义一个key,作为日志输出的标记;key支持SpEL表达式

# 1. AOP切面

注解定义

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
    String key();
}
1
2
3
4
5

切面逻辑

@Slf4j
@Aspect
@Component
public class AopAspect implements ApplicationContextAware {
    private ExpressionParser parser = new SpelExpressionParser();
    private ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();

    @Around("@annotation(logAno)")
    public Object around(ProceedingJoinPoint joinPoint, Log logAno) throws Throwable {
        long start = System.currentTimeMillis();
        String key = loadKey(logAno.key(), joinPoint);
        try {
            return joinPoint.proceed();
        } finally {
            log.info("key: {}, args: {}, cost: {}", key,
                    JSONObject.toJSONString(joinPoint.getArgs()),
                    System.currentTimeMillis() - start);
        }
    }

    private String loadKey(String key, ProceedingJoinPoint joinPoint) {
        if (key == null) {
            return key;
        }

        StandardEvaluationContext context = new StandardEvaluationContext();

        context.setBeanResolver(new BeanFactoryResolver(applicationContext));
        String[] params = parameterNameDiscoverer.getParameterNames(((MethodSignature) joinPoint.getSignature()).getMethod());
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            context.setVariable(params[i], args[i]);
        }

        return parser.parseExpression(key).getValue(context, String.class);
    }

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

上面这个逻辑比较简单,和大家熟知的使用姿势没有太大的区别

# 2. StandardEvaluationContext安全问题

关于StandardEvaluationContext的注入问题,有兴趣的可以查询一下相关文章;对于安全校验较高的,要求只能使用SimpleEvaluationContext,使用它的话,SpEL的能力就被限制了

如加一个测试

@Data
@Accessors(chain = true)
public class DemoDo {

    private String name;

    private Integer age;
}
1
2
3
4
5
6
7
8

服务类

@Service
public class HelloService {

    @Log(key = "#demo.getName()")
    public String say(DemoDo demo, String prefix) {
        return prefix + ":" + demo;
    }
}
1
2
3
4
5
6
7
8

为了验证SimpleEvaluationContext,我们修改一下上面的loadKeys方法

private String loadKey(String key, ProceedingJoinPoint joinPoint) {
    if (key == null) {
        return key;
    }

    SimpleEvaluationContext context = new SimpleEvaluationContext.Builder().build();
    String[] params = parameterNameDiscoverer.getParameterNames(((MethodSignature) joinPoint.getSignature()).getMethod());
    Object[] args = joinPoint.getArgs();
    for (int i = 0; i < args.length; i++) {
        context.setVariable(params[i], args[i]);
    }

    return parser.parseExpression(key).getValue(context, String.class);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

启动测试

@SpringBootApplication
public class Application {

    public Application(HelloService helloService) {
        helloService.say(new DemoDo().setName("一灰灰blog").setAge(18), "welcome");
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}
1
2
3
4
5
6
7
8
9
10
11

直接提示方法找不到!!!

# 3. gson序列化问题

上面的case中,使用的FastJson对传参进行序列化,接下来我们采用Gson来做序列化

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
</dependency>
1
2
3
4

然后新增一个特殊的方法

@Service
public class HelloService {
    /**
     * 字面量,注意用单引号包裹起来
     * @param key
     * @return
     */
    @Log(key = "'yihuihuiblog'")
    public String hello(String key, HelloService helloService) {
        return key + "_" + helloService.say(new DemoDo().setName(key).setAge(10), "prefix");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

注意上面方法的第二个参数,非常有意思的是,传参是自己的实例;再次执行

public Application(HelloService helloService) {
    helloService.say(new DemoDo().setName("一灰灰blog").setAge(18), "welcome");

    String ans = helloService.hello("一灰灰", helloService);
    System.out.println(ans);
}
1
2
3
4
5
6

直接抛了异常

这就很尴尬了,一个输出日志的辅助工具,因为序列化直接导致接口不可用,这就不优雅了;而我们作为日志输出的切面,又是没有办法控制这个传参的,没办法要求使用的参数,一定能序列化,这里需要额外注意 (比较好的方式就是简单对象都实现toString,然后输出toString的结果;而不是json串)

# 4. 小结

虽然上面一大串的内容,总结下来,也就两点

  • SpEL若采用的是SimpleEvaluationContext,那么注意spel的功能是减弱的,一些特性不支持
  • 若将方法参数json序列化输出,那么需要注意某些类在序列化的过程中,可能会抛异常

(看到这里的小伙伴,不妨点个赞,顺手关注下微信公众号”一灰灰blog“,我的公众号已经寂寞的长草了😭)

# III. 不能错过的源码和相关知识点

# 0. 项目

  • 工程:https://github.com/liuyueyi/spring-boot-demo (opens new window)
  • 源码: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/014-spel-aop (opens new window)

AOP系列博文

  • SpringBoot基础系列AOP无法拦截接口上注解场景兼容 (opens new window)
  • SpringBoot基础系列实现一个简单的分布式定时任务(应用篇) (opens new window)
  • SpringBoot基础篇AOP之拦截优先级详解 (opens new window)
  • SpringBoot应用篇之AOP实现日志功能 (opens new window)
  • SpringBoot基础篇AOP之高级使用技能 (opens new window)
  • SpringBoot基础篇AOP之基本使用姿势小结 (opens new window)

# 1. 一灰灰Blog

尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

  • 一灰灰Blog个人博客 https://blog.hhui.top (opens new window)
  • 一灰灰Blog-Spring专题博客 http://spring.hhui.top (opens new window)

一灰灰blog

编辑 (opens new window)
#AOP
上次更新: 2021/10/15, 19:56:22
【基础系列】接口上注解AOP拦截不到场景兼容
【基础系列】Bean之基本定义与使用

← 【基础系列】接口上注解AOP拦截不到场景兼容 【基础系列】Bean之基本定义与使用→

最近更新
01
【基础系列】基于maven多环境配置
04-25
02
【WEB系列】内嵌Tomcat配置Accesslog日志文件生成位置源码探索
04-24
03
【搜索系列】ES查询常用实例演示
04-18
更多文章>
Theme by Vdoing | Copyright © 2017-2022 一灰灰Blog
MIT License | 鄂ICP备18017282号 |
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×