为什么没有为Jersey控制器检测到我的Aspect(使用自定义注释)? [英] Why my Aspect is not detected for Jersey controller (using custom annotation)?
问题描述
我想在Jersey控制器上创建一个Aspect,以测量执行服务所花费的时间.我正在与切入点作斗争,因为它没有被检测到并且我的方面从未启动过.
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);
}
}
我的Monitor自定义注释为
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一起使用的原因,因为在这种特殊情况下我必须使用编织器.
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.
可以在以下答案中找到配置编织器的全部详细信息: 使用Aspectj-maven-为Java 8进行Spring + AspectJ编织插件
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(使用自定义注释)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!