我想在我的函数中使用@before
,@after
和@AfterThrowing
。
如果其他功能使用了@MyAspectTest
之类的注释,则应在相关时间运行beforeAction()
,afterAction()
和afterExcept()
。
但是,这似乎不起作用。
我已经输入了依赖项并修改了bean。
package com.service.metrics;
import com.mgr.CMPMgr;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.AfterThrowing;
import java.lang.reflect.Method;
@Aspect
public class CMPAspect {
@Before(value="@annotation(com.mgr.CMPMgr)")
public void beforeAction(JoinPoint joinPoint) throws ClassNotFoundException {
testcode
}
@After(value="@annotation(com.mgr.CMPMgr)")
public void afterAction(){
testcode
}
@AfterThrowing(value="@annotation(com.mgr.CMPMgr)")
public void afterExcept(){
testcode
}
}
package com.mgr;
public @interface CMPMgr {
String name() default "";
long startTime = System.currentTimeMillis();
}
@CMPMgr(name = "vipGet")
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/{lbId}")
public Response get(@PathParam("lbId")String lbId,
@HeaderParam("Authorization") String basicAuthData,
@HeaderParam("UserID") String behalf) {
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.0</version>
</dependency>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy />
<!-- enabling annotation driven configuration / -->
<context:annotation-config />
<context:component-scan />
<!-- responsible for registering the necessary Spring components that power
annotation-driven transaction management; such as when @Transactional methods
are invoked -->
<tx:annotation-driven />
它应该进入Aspect函数。但是我发现它不能在调试模式下工作。为什么?
答案 0 :(得分:2)
一些事情浮现在脑海:
@Retention(RetentionPolicy.RUNTIME)
。@Component
,但我也看不到相应的注释。public Response get(..)
的目标类也必须是Spring bean /组件。因为您只显示不连贯的代码片段,而不是完整的类定义,所以我不知道该类驻留在哪个包中(如果它是Spring组件)以及是否由组件扫描来拾取。