我正在尝试记录使用自定义接口(interface)注释的方法的执行时间。
我正在使用 Spring AOP。
但这似乎不适用于内部方法。
我觉得是Spring AOP的局限性
@Aspect
public class BusinessProfiler {
private static Log log = LogFactory.getLog(BusinessProfiler.class);
@Around("execution(* *(..)) && @annotation(TimeLog)")
public Object profile(ProceedingJoinPoint point) throws Throwable {
long start = System.currentTimeMillis();
Object result = point.proceed();
String format =
String.format("%s#%s: took [%s msec]", point.getTarget().getClass().getSimpleName(),
MethodSignature.class.cast(point.getSignature()).getMethod().getName(),
System.currentTimeMillis() - start);
log.info(format);
return result;
}
}
除了Spring AOP还有其他选择吗
最佳答案
如果您考虑一下 Spring 处理 AOP 注释的方式,就会很清楚:
Spring 获取您的类并将其包装在代理中,并添加由 AOP 注释动态生成的额外代码。因此,只有通过代理调用的代码(即从您的类(class)外部调用的代码)才会被包括在内。
例子
@Service
public class Foo {
public void doSomething() {
doSomethingInternal();
}
public void doSomethingInternal() {
}
}
如果从另一个 Spring bean 我这样做:
@Service
public class Bar {
@Autowired
private Foo foo;
public void execute() {
foo.doSomething();
}
}
只有 doSomething 会通过包装您的类的代理调用,而不会被您的类调用的 doSomethingInternal。
关于java - 用于内部方法和私有(private)方法的 AOP Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42832946/