【发布时间】:2014-04-07 14:08:30
【问题描述】:
有没有办法用 Spring 添加一个记录器实例? 有没有办法在我的自定义代码中跟踪每个方法调用?
我通常这样做:
package my.java.code;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class A {
// How to add this line with Spring ?
private static final Logger logger = LoggerFactory.getLogger(A.class);
public void A() {
// How to add this line with Spring ?
logger.trace("");
// Do something...
}
public void A(Object o) {
// How to add this line with Spring ?
logger.trace("{}", o);
// Do something...
}
public void method1() {
// How to add this line with Spring ?
logger.trace("");
// Do something...
}
public void method2(Object o) {
// How to add this line with Spring ?
logger.trace("{}", o);
// Do something...
}
}
有没有办法用 Spring 来简化这个?
目标是:
- 避免重复代码
【问题讨论】:
-
我不明白这个问题。你有什么问题?
-
你想摆脱重复的代码吗?
-
使用 Spring AOP 和
CustomizableTraceInterceptor。另见stackoverflow.com/questions/13182205/…