[英]How to intercept each method call within given method using Spring AOP or AspectJ
class Test {
@override
public String a(){
b();
d();
}
private String b() {
c();
}
private String c(){
d();
}
private String d(){}
}
我想拦截从重写方法 A() 调用的 Test 类的每个方法,并想知道 b()、c() 等每个方法在单独处理某些业务逻辑时花费了多少时间。
如何使用 Spring AOP 或 Aspectj 实现它?
为了
您需要使用 LTW(加载时编织)从 Spring AOP(基于代理,许多限制,缓慢)切换到AspectJ ,如 Spring 手册中所述。
这是一个纯 AspectJ(没有 Spring,只有 Java SE)的例子,你可以很容易地适应你的需求:
示例界面
package de.scrum_master.app;
public interface TextTransformer {
String transform(String text);
}
类实现接口包括。 main
方法:
如您所见,我编写了一个像您这样的示例,并且还让方法花时间以便稍后在方面进行测量:
package de.scrum_master.app;
public class Application implements TextTransformer {
@Override
public String transform(String text) {
String geekSpelling;
try {
geekSpelling = toGeekSpelling(text);
return toUpperCase(geekSpelling);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private String toGeekSpelling(String text) throws InterruptedException {
Thread.sleep(100);
return replaceVovels(text).replaceAll("[lL]", "1");
}
private String replaceVovels(String text) throws InterruptedException {
Thread.sleep(75);
return text.replaceAll("[oO]", "0").replaceAll("[eE]", "Ɛ");
}
private String toUpperCase(String text) throws InterruptedException {
Thread.sleep(50);
return text.toUpperCase();
}
public static void main(String[] args) throws InterruptedException {
System.out.println(new Application().transform("Hello world!"));
}
}
方面:
package de.scrum_master.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import static java.lang.System.currentTimeMillis;
@Aspect
public class TimingAspect {
@Around("execution(* *(..)) && cflow(execution(* de.scrum_master.app.TextTransformer.*(..)))")
public Object measureExecutionTime(ProceedingJoinPoint thisJoinPoint) throws Throwable {
long startTime = currentTimeMillis();
Object result = thisJoinPoint.proceed();
System.out.println(thisJoinPoint + " -> " + (currentTimeMillis() - startTime) + " ms");
return result;
}
}
控制台日志:
execution(String de.scrum_master.app.Application.replaceVovels(String)) -> 75 ms
execution(String de.scrum_master.app.Application.toGeekSpelling(String)) -> 189 ms
execution(String de.scrum_master.app.Application.toUpperCase(String)) -> 63 ms
execution(String de.scrum_master.app.Application.transform(String)) -> 252 ms
HƐ110 W0R1D!
您还可以通过将切入点从cflow()
更改为cflowbelow()
来排除transform(..)
方法:
@Around("execution(* *(..)) && cflowbelow(execution(* de.scrum_master.app.TextTransformer.*(..)))")
那么控制台日志就是:
execution(String de.scrum_master.app.Application.replaceVovels(String)) -> 77 ms
execution(String de.scrum_master.app.Application.toGeekSpelling(String)) -> 179 ms
execution(String de.scrum_master.app.Application.toUpperCase(String)) -> 62 ms
HƐ110 W0R1D!
顺便说一句,请务必阅读 AspectJ 和/或 Spring AOP 手册。
Spring AOP使用代理,当你从外部调用bean的方法时,使用代理并且可以拦截该方法,但是当你从类内部调用方法时,不使用代理而使用类直接。
你有三个选择
如果使用公共方法没有问题,第一个也是简单的方法是将函数b()
、 c()
和d()
移到另一个 bean 中。 这样每次调用这个方法都会被拦截。
public class Test {
public String a() { ... }
}
public class Test2 {
public String b() { ... }
public String c() { ... }
public String d() { ... }
}
如果您想将所有内容都保存在同一个文件中,您也可以将其用作内部静态类。
public class Test {
public String a() { ... }
public static class Test2 {
public String b() { ... }
public String c() { ... }
public String d() { ... }
}
}
您应该在 Test 的构造函数中自动装配 Test2。
public class Test {
private final Test2 test2;
@Autowired public Test(final Test2 test2) {
this.test2 = test2;
}
public String a() {
test2.b();
test2.c();
test2.d();
}
}
最后创建 around 方法。
@Around(value = "execution(* package.of.the.class.Test.*(..))")
public Object aroundA(ProceedingJoinPoint pjp) throws Throwable { ... }
@Around(value = "execution(* package.of.the.class.Test2.*(..))")
public Object aroundBCD(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
Object output = pjp.proceed();
long elapsedTime = System.currentTimeMillis() - start;
// perform side efects with elapsed time e.g. print, store...
return output;
}
或者类似的东西
@Around(value = "execution(* package.of.the.class.Test.*(..)) || " +
"execution(* package.of.the.class.Test2.*(..))")
public Object aroundABCD(ProceedingJoinPoint pjp) throws Throwable { ... }
第二种选择是使用 CGLIB bean、封装私有方法和自注入。
您只需使用范围注释声明一个 CGLIB bean
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
@Bean public Test test() {
return new Test();
}
自注入和封装私有方法如下
public class Test {
@Autowired private Test test;
// ...
public String a() {
test.b(); // call through proxy (it is intercepted)
}
String b() { ... } // package private method
// ...
}
第三种解决方案是使用 LWT Load-Time 编织,即使用 aspectj 而不是 spring aop。 这允许拦截甚至在同一个类中的方法调用。 您可以使用官方 spring 文档来实现它,但您必须使用 java 代理才能运行。
调用方式
如果您需要知道特定方法是否调用了被拦截的函数,您可以使用Thread.currentThread().getStackTrace()
作为选项 1 或 2。如果您使用 aspectj(选项 3),您可以拦截这些方法与cflow()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.