spring - Spring AOP:仅从另一个方法调用的捕获方法
问题描述
猜猜我有这些方法:
@Service
public class Service1 {
private @Autowired Service2 service2;
public void method1() {
this.service2.method2();
}
}
@Service2
public class Service2 {
public void method2() {
// Do something
}
}
我想知道如何捕获Service2.method2()
呼叫,当它被调用时Service1.method1())
。
有任何想法吗?
解决方案
您可以在 Spring 中使用 AspectJ而不是 Spring AOP vis compile-time 或 load-time weaving,然后使用类似切入点
execution(* my.package.Service2.method2()) &&
cflow(execution(* my.package.Service2.method2()))
或者,类似于@Damith所说,使用
Thread.currentThread().getStackTrace()
,new Exception().getStackTrace()
或者- Java 9
StackWalker
API
为了手动导航调用堆栈并找到您正在寻找的信息。我的建议是使用 AspectJ,不仅因为它的优雅,还因为它的效率。
推荐阅读
- bash - if/else 测试 bash 中 case 控制语句的等效项
- r - 如何有效地加载 xlsx 文件
- hadoop - 配置大型 Hive 导入作业
- r - 获取最新记录,但如果最新为空则获取最后一条最新记录
- ruby-on-rails - 在 Rails 应用程序中使用耐嚼的 gem 更新用户索引失败
- encryption - Base64 扩展密钥的加密强度
- javascript - 我的 firebase 保存数据有一些错误
- java - Firebase 在每个循环之后给出空值
- python - Itertool 组合 Python 数据框
- java - Java中的轻量级XLSX阅读器