public class SomeService {
public void someMethod(){
// this will fetch all the information about current thread
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
// third element of the array will contain caller class name
String callerClassName = stackTrace[2].getClassName();
// now you can perform whatever operation based on callerClassName
if(callerClassName.equals("A")){
//doThis
}else{
//doThat
}
}
}
2条答案
按热度按时间jgovgodb1#
你可以使用反射技巧来解决这个问题,但是真的没有理由这样做。解决方案很简单:如果你需要不同的功能,使用不同的方法:
字符串
更新:我认为这是显而易见的,但现在你可以使用它:
型
rjee0c152#
我对Spring生态系统没有任何概念,但Java本身有一个解决方案。
字符串