我在 AspectJ 实施方面遇到一些问题!
我想为带有 @MyAnnotation 注释的方法创建一个日志方法。
MyAnnotation.java:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation{ }
MyAspect.java:
@Aspect
public class MyAspect {
private static Logger logger = Logger.getLogger(MyAspect.class.getName());
@Pointcut("@annotation(com.utils.aop.annotations.MyAnnotation)")
public void logMyAspect() {
}
@Before("logMyAspect()")
public void logMethod(JoinPoint jp) {
String methodName = jp.getSignature().getName();
logger.info("Executing method: " + methodName);
}
}
我在项目的一些服务方法之前使用我的@MyAnnotation:
@RolesAllowed({ "DEV", "GUI", "API" })
@POST
@Path("/getList")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@MyAnnotation
public Response getList(@Context final ContainerRequestContext requestContext,
FilterAndSortObject filterAndSortObject,
@QueryParam("offset") final int offset,
@QueryParam("limit") final int limit)
{
...
}
我还发现我应该在配置类中使用 @EnableAspectJAutoProxy :
@Configuration
@EnableAspectJAutoProxy
public class ServletContextClass implements ServletContextListener {
final static Logger logger = Logger.getLogger(ServletContextClass.class);
@Override
public void contextInitialized(final ServletContextEvent sce) {
...
}
...
}
然而它似乎不起作用。它不记录任何内容!
我在中使用了断点logMethod(JoinPoint jp)
也检查了结果,没有成功!
有谁知道为什么这不起作用?