问题描述
我想实现记录使用某些注释注释的某些方法的进入和退出的功能(例如:@Loggable
).我遇到了AspectJ AOP,我们可以使用它进行操作.
I want to implement the ability to log entry and exit of some methods that are annotated with some annotation (For example: @Loggable
). I came across AspectJ AOP using which we can do this.
我实现了自己的自定义方面,以自定义要在使用@Loggable
调用的方法的进入和退出时打印的日志消息:
I implemented a custom aspect of my own to customise the log message I want to print on entry and exit of the method that gets called with @Loggable
:
@Aspect
public final class MethodLogger {
private static final Logger LOG = LoggerFactory.getLogger(MethodLogger.class);
@Around("execution(* *(..)) && @annotation(Loggable)")
public Object around(ProceedingJoinPoint point) throws Throwable {
String className = MethodSignature.class.cast(point.getSignature()).getClass().getName();
String methodName = MethodSignature.class.cast(point.getSignature()).getMethod().getName();
LOG.info(
"Entering method:{}() of class:{} with parameters: {}",
methodName,
className,
Arrays.toString(point.getArgs()));
try
{
return point.proceed();
}
catch(Throwable e){
throw e;
}
finally
{
LOG.info(
"Exiting method:{}() of class:{} with parameters: {}",
methodName,
className,
Arrays.toString(point.getArgs()));
}
}
}
pom.xml依赖项:
pom.xml dependencies:
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-aspects</artifactId>
<version>0.22.5</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.1</version>
</dependency>
类的方法带有@Loggable
注释:
@Component
public class LoginPage extends BasePage {
@Loggable
public Object login(String username, String password) {
}
}
问题:
主要通过以下方式调用此实例方法(login()
)时:loginPage.login()
,我看不到进入和退出日志正在打印到日志输出中.
When this instance method (login()
)is called mostly in the following manner:loginPage.login()
, I cannot see the entry and exit log being printed to the log output.
请注意:
- 我正在使用Spring依赖注入使用
@Component
批注初始化此类,但不确定这是否对论坛有用,但仍然让大家知道. - 这是一个测试自动化项目,我从JUnit + Cucumber运行器类中触发了一些UI自动化测试.
- 我没有从Maven触发测试.
- I am using Spring dependency injection to initialise such classes with
@Component
annotation, not sure if that is useful information for the forum but still letting you all know. - This is a test automation project where I am triggering some UI automated tests from a JUnit+Cucumber runner class.
- I am not triggering my tests from maven.
有人可以建议这里出什么问题吗?
Can someone suggest what could be going wrong here?
推荐答案
您的方面MethodLogger
尚未初始化.尝试将@Component
添加到MethodLogger
.这应该将方面加载并注册到上下文中,以使其可以使用.
You aspect MethodLogger
is not initialized yet.Try adding @Component
to the MethodLogger
. This should load and register the aspect into the context, making it ready for use.
此外,您可能想简化切点@Around("execution(* *(..)) && @annotation(Loggable)")
.
Also you might want to simplify your point cut @Around("execution(* *(..)) && @annotation(Loggable)")
.
execution(* *(..))
基本上是通配符,因此没有用.只需使用@Around("@annotation(Loggable)")
.
execution(* *(..))
is basically a wild card and therefore useless. Just use@Around("@annotation(Loggable)")
.
这篇关于带有@Loggable批注的方法从不打印方面日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!