问题描述
我想在 Jersey 控制器上创建一个方面来衡量服务执行所需的时间.我正在与我的切入点作斗争,因为它没有被检测到,我的方面也从未被启动.
I want to create an Aspect over a Jersey controller to measure how long the services take to be executed. I'm fighting against my pointcut since it isn't detected and my aspect never gets launched.
我尝试过使用很多切入点,例如:
I have tried using lots of pointcuts like:
execution(@Monitor * *.*(..))
execution(public * *(..))
change the order of @Aspect and @Component
Added a pointcut like this:
@Pointcut("execution(@Monitor * *.*(..))")
public void monitorRequestTargets(){}
@Around("monitorRequestTargets()")
Tried using AOP and CGLIB
<aop:aspectj-autoproxy proxy-target-class="true"/>
Also tried changing the order of configuration in context.xml
Eclipse 检测到我的方法正在由我的方面提供建议,但它不会在运行时执行.你能给我任何关于为什么没有创建方面或切入点没有启动的提示吗?
Eclipse detects that my methods are being adviced by my aspect but it isn't executed at runtime. Could you give me any hint about why aspect is not created or the pointcut doesn't launch?
我的代码如下.
Context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- Enables the Spring MVC @Controller -->
<annotation-driven />
<!-- Enables AspectJ -->
<aop:aspectj-autoproxy />
<!-- .....more definitions -->
<context:component-scan base-package="com.mypackage" />
</beans:beans>
我的MonitorAspect
@Component
@Aspect
public class MonitorAspect
{
private static final Logger logger = LoggerFactory.getLogger(MonitorAspect.class);
@Around("@annotation(com.mypackage.Monitor)")
public void logTimeUsage(ProceedingJoinPoint joinPoint) throws Throwable
{
// Store executing method
String method = joinPoint.getSignature().getName();
// Track time
long startTime = System.currentTimeMillis();
joinPoint.proceed();
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
// Log time consumed by executing method
logger.info(method + ": " + duration);
}
}
我的监视器自定义注释是
My Monitor custom annotation is
@Target(value = {ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Monitor
{
}
我想使用方面的控制器:
My controller which I want to use the aspect:
@Monitor
@POST
@Consumes("application/json")
@Produces("application/json")
@Path("/{tkn}/test/")
public Response test(
@Context HttpServletRequest httpReq,
@Context UriInfo uri,
String enrollReqJson
) {
Thread.sleep(1000); // Implementation is not important
}
我的pom.xml
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Jersey / Spring -->
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>1.14</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.8</version>
</dependency>
<!-- Spring AOP / AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
推荐答案
我发现了为什么我的方面不能与 Jersey 一起使用,因为我必须在这个特殊情况下使用 weaver.
I found why my aspects weren't working with Jersey because I had to use the weaver for this particular case.
在配置 aspectj-maven-plugin 以编织我的方面后,一切正常.
After configuring aspectj-maven-plugin to weave my aspects everything works successfully.
可以在此答案中找到配置编织器的完整详细信息:Spring + AspectJ 织入 java 8 使用 aspectj-maven-插件
The full details to configure the weaver can be found on this answer:Spring + AspectJ weaving for java 8 using aspectj-maven-plugin
这篇关于为什么 Jersey 控制器没有检测到我的 Aspect(使用自定义注释)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!