该服务是一个spring bean,它是@autowired 进入我的班级:
public class MyClass {
@Autowired
private Service service;
private void myMethod() {
service.call();
}
}
call() 方法可能会抛出异常,所以我用 try-catch 行包装它:
public class MyClass {
@Autowired
private Service service;
private void myMethod() {
try {
service.call();
} catch (Exception e) {
// do some logging
}
}
}
如何编写自定义注释并自动执行此操作?像这样:
public class MyClass {
@Monitored
@Autowired
private Service service;
private void myMethod() {
// it will be logged if call() throws exception
service.call();
}
}
谢谢你的帮助。