我在 AspectJ 实现方面遇到一些问题!
我想为带有 @MyAnnotation 注释的方法创建一个日志方法。
MyAnnotation.java:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation{ }
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我在项目的一些服务方法之前使用我的@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)
{
...
} …Run Code Online (Sandbox Code Playgroud)