【SpringBoot 根底系列】AOP联合SpEL实现日志输入的注意事项一二
应用 AOP 来打印日志大家一把都很相熟了,最近在应用的过程中,发现了几个有意思的问题,一个是 SpEL 的解析,一个是参数的 JSON 格局输入
<!-- more -->
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>
II. AOP & SpEL
对于 AOP 与 SpEL 的知识点,之前都有过专门的介绍,这里做一个聚合,一个非常简单的日志输入切面,在须要打印日志的办法上,增加注解@Log
,这个注解中定义一个key
,作为日志输入的标记;key 反对 SpEL 表达式
1. AOP 切面
注解定义
@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface Log { String key();}
切面逻辑
@Slf4j@Aspect@Componentpublic 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; }}
下面这个逻辑比较简单,和大家熟知的应用姿态没有太大的区别
2. StandardEvaluationContext 平安问题
对于StandardEvaluationContext
的注入问题,有趣味的能够查问一下相干文章;对于平安校验较高的,要求只能应用SimpleEvaluationContext
,应用它的话,SpEL 的能力就被限度了
如加一个测试
@Data@Accessors(chain = true)public class DemoDo { private String name; private Integer age;}
服务类
@Servicepublic class HelloService { @Log(key = "#demo.getName()") public String say(DemoDo demo, String prefix) { return prefix + ":" + demo; }}
为了验证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);}
启动测试
@SpringBootApplicationpublic 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); }}
间接提醒办法找不到!!!
3. gson 序列化问题
下面的 case 中,应用的 FastJson 对传参进行序列化,接下来咱们采纳 Gson 来做序列化
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId></dependency>
而后新增一个非凡的办法
@Servicepublic 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"); }}
留神下面办法的第二个参数,十分有意思的是,传参是本人的实例;再次执行
public Application(HelloService helloService) { helloService.say(new DemoDo().setName("一灰灰blog").setAge(18), "welcome"); String ans = helloService.hello("一灰灰", helloService); System.out.println(ans);}
间接抛了异样
这就很难堪了,一个输入日志的辅助工具,因为序列化间接导致接口不可用,这就不优雅了;而咱们作为日志输入的切面,又是没有方法管制这个传参的,没方法要求应用的参数,肯定能序列化,这里须要额定留神 (比拟好的形式就是简略对象都实现 toString,而后输入 toString 的后果;而不是 json 串)
4. 小结
尽管下面一大串的内容,总结下来,也就两点
- SpEL 若采纳的是
SimpleEvaluationContext
,那么留神 spel 的性能是削弱的,一些个性不反对 - 若将办法参数 json 序列化输入,那么须要留神某些类在序列化的过程中,可能会抛异样
(看到这里的小伙伴,无妨点个赞,棘手关注下微信公众号”一灰灰 blog“,我的公众号曾经寂寞的长草了 )
III. 不能错过的源码和相干知识点
0. 我的项目
- 工程:https://github.com/liuyueyi/spring-boot-demo
- 源码: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/014-spel-aop
AOP 系列博文
- SpringBoot 根底系列 AOP 无奈拦挡接口上注解场景兼容
- SpringBoot 根底系列实现一个简略的分布式定时工作(利用篇)
- SpringBoot 根底篇 AOP 之拦挡优先级详解
- SpringBoot 利用篇之 AOP 实现日志性能
- SpringBoot 根底篇 AOP 之高级应用技能
- SpringBoot 根底篇 AOP 之根本应用姿态小结
1. 一灰灰 Blog
尽信书则不如,以上内容,纯属一家之言,因集体能力无限,不免有疏漏和谬误之处,如发现 bug 或者有更好的倡议,欢送批评指正,不吝感谢
上面一灰灰的集体博客,记录所有学习和工作中的博文,欢送大家前去逛逛
- 一灰灰 Blog 集体博客 https://blog.hhui.top
- 一灰灰 Blog-Spring 专题博客 http://spring.hhui.top