作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在我的函数中使用 @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 函数。但我发现它在 Debug模式下不起作用。为什么?
最佳答案
我想到了一些事情:
@Retention(RetentionPolicy.RUNTIME)
。@Component
,但我也没有看到相应的注释。public Response get(..)
的目标类也必须是 Spring bean/组件。因为您只显示不连贯的片段而不是完整的类定义,所以我不知道该类驻留在哪个包中,如果它是 Spring 组件,以及它是否被组件扫描拾取。关于java - Spring AOP切面不拦截带注释的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57456474/
我的 AspectJ 方面看起来像这样: @Aspect public class MyAspect { private Child child; public MyAspect() {
@Around注解可以用来在调用一个具体方法前和调用后来完成一些具体的任务。 比如我们想在执行controller中方法前打印出请求参数,并在方法执行结束后来打印出响应值,这个时候,我们就可以借助
我需要避免在对类进行单元测试时调用某个方面。 我正在使用 Java 8、spring 4.3.22.RELEASE 和 mockito。我有一个@Service 和一个单元测试。我还有一个 @Aspe
我在一个 Maven 项目模块中有一个方面 com.x.NiceAspect,在一个单独的 Maven 模块中有一个类 com.x.NiceClass。这些模块具有相同的 POM 父级,一起创建一个项
我是一名优秀的程序员,十分优秀!