Skip to content

Commit

Permalink
#177 aop & spel注意事项
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyueyi committed Jun 8, 2021
1 parent 99f3d0d commit 0ee9c1f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
4 changes: 4 additions & 0 deletions spring-boot/014-spel-aop/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit 0ee9c1f

Please sign in to comment.