From 0ee9c1f6322d7ad2a3502d5f921e893c736f124a Mon Sep 17 00:00:00 2001 From: yihui Date: Tue, 8 Jun 2021 19:46:49 +0800 Subject: [PATCH] =?UTF-8?q?#177=20aop=20&=20spel=E6=B3=A8=E6=84=8F?= =?UTF-8?q?=E4=BA=8B=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-boot/014-spel-aop/pom.xml | 4 ++++ .../java/com/git/hui/boot/spel/aop/Application.java | 3 +++ .../java/com/git/hui/boot/spel/aop/aop/AopAspect.java | 11 ++++++----- .../git/hui/boot/spel/aop/service/HelloService.java | 10 ++++++++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/spring-boot/014-spel-aop/pom.xml b/spring-boot/014-spel-aop/pom.xml index 1e1ee7f9..f14fa1e5 100644 --- a/spring-boot/014-spel-aop/pom.xml +++ b/spring-boot/014-spel-aop/pom.xml @@ -15,6 +15,10 @@ org.aspectj aspectjweaver + + com.google.code.gson + gson + diff --git a/spring-boot/014-spel-aop/src/main/java/com/git/hui/boot/spel/aop/Application.java b/spring-boot/014-spel-aop/src/main/java/com/git/hui/boot/spel/aop/Application.java index 5f3aafcb..6f1d9e5c 100644 --- a/spring-boot/014-spel-aop/src/main/java/com/git/hui/boot/spel/aop/Application.java +++ b/spring-boot/014-spel-aop/src/main/java/com/git/hui/boot/spel/aop/Application.java @@ -14,6 +14,9 @@ public class Application { public Application(HelloService helloService) { helloService.say(new DemoDo().setName("一灰灰blog").setAge(18), "welcome"); + + String ans = helloService.hello("一灰灰", helloService); + System.out.println(ans); } public static void main(String[] args) { diff --git a/spring-boot/014-spel-aop/src/main/java/com/git/hui/boot/spel/aop/aop/AopAspect.java b/spring-boot/014-spel-aop/src/main/java/com/git/hui/boot/spel/aop/aop/AopAspect.java index 2ca93336..e78d23a3 100644 --- a/spring-boot/014-spel-aop/src/main/java/com/git/hui/boot/spel/aop/aop/AopAspect.java +++ b/spring-boot/014-spel-aop/src/main/java/com/git/hui/boot/spel/aop/aop/AopAspect.java @@ -1,5 +1,7 @@ package com.git.hui.boot.spel.aop.aop; +import com.alibaba.fastjson.JSONObject; +import com.google.gson.Gson; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; @@ -11,15 +13,11 @@ import org.springframework.context.expression.BeanFactoryResolver; import org.springframework.core.DefaultParameterNameDiscoverer; import org.springframework.core.ParameterNameDiscoverer; -import org.springframework.expression.EvaluationContext; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; -import org.springframework.expression.spel.support.SimpleEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.stereotype.Component; -import java.util.Arrays; - /** * @author yihui * @date 21/6/4 @@ -33,11 +31,14 @@ public class AopAspect implements ApplicationContextAware { @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: {}", key, Arrays.asList(joinPoint.getArgs())); + log.info("key: {}, args: {}, cost: {}", key, + new Gson().toJson(joinPoint.getArgs()), + System.currentTimeMillis() - start); } } diff --git a/spring-boot/014-spel-aop/src/main/java/com/git/hui/boot/spel/aop/service/HelloService.java b/spring-boot/014-spel-aop/src/main/java/com/git/hui/boot/spel/aop/service/HelloService.java index ac340030..16800066 100644 --- a/spring-boot/014-spel-aop/src/main/java/com/git/hui/boot/spel/aop/service/HelloService.java +++ b/spring-boot/014-spel-aop/src/main/java/com/git/hui/boot/spel/aop/service/HelloService.java @@ -14,4 +14,14 @@ public class HelloService { public String say(DemoDo demo, String prefix) { return prefix + ":" + demo; } + + /** + * 字面量,注意用单引号包裹起来 + * @param key + * @return + */ + @Log(key = "'yihuihuiblog'") + public String hello(String key, HelloService helloService) { + return key + "_" + helloService.say(new DemoDo().setName(key).setAge(10), "prefix"); + } }