- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试学习 AspectJ 并弄清楚如何检索流程中某些点的特定连接点。我的例子是这样的:
我想运行一个用 JUnit 的 @Test
注释的单元测试,然后在该测试中调用的任何方法可能位于用另一个注释注释的另一个类中,比方说 @Example
,然后我基本上可以在这些特定点访问整个流程,因此我能够获取用 @Test
注释的测试的类名/方法名,然后还获取该测试的方法信息方法注释为@Example
。我提供了一些示例代码以供澄清:
测试类:
public class ExampleTest {
@Test
public void shouldTestSomething() {
ExampleClass exampleClazz = new ExampleClass();
exampleClazz.something();
// ...
}
POJO:
public class ExampleClass {
@Example
public void something() {
// do something
end
end
因此,通过这些类,我想创建一个方面,基本上可以找到在 @Test
中调用的任何类型的 @Example
,这样我就可以访问我可以在两个(或多个)连接点中从 AspectJ JoinPoint
对象获取方法/类签名。
我尝试过这样的事情:
@Aspect
public class ExampleAspect {
@Pointcut("execution(@org.junit.Test * *(..))
&& !within(ExampleAspect)")
public void testPointCut() {}
@Pointcut("@annotation(com.example.Example)")
public void examplePointCut() {}
@After("cflow(testPointCut) && examplePointCut()")
public void doSomething(JoinPoint joinPoint) {
System.out.println(joinPoint.getSignature());
}
}
但是输出看起来像这样:
void ExampleTest.ExampleClass.something()
主要问题是签名中缺少测试方法的名称(shouldTestSomething())。检索它的最佳方法是什么?
最佳答案
不确定我是否理解正确,但如果您需要访问有关调用您感兴趣的连接点下的一段代码的上下文的信息,那么您需要的是 thisEnendingJoinPointStaticPart
(使用 native AspectJ 语法)。如果您使用 AspectJ 5 注释样式方面,只需向您的通知方法添加一个类型为 JoinPoint.EnendingStaticPart
的参数。
请注意,这不适用于 execution()
样式切入点,仅适用于 call()
样式切入点,否则 JoinPoint.EnendingStaticPart
和 JoinPoint.StaticPart
将是相同的。
这意味着您需要按以下方式重写您的方面:
@Aspect
public class ExampleAspect {
@Pointcut("execution(@org.junit.Test * *(..)) && !within(ExampleAspect)")
public void testPointCut() {
}
@Pointcut("call(@com.example.Example * *(..))")
public void examplePointCut() {
}
@After("cflow(testPointCut()) && examplePointCut()")
public void doSomething(JoinPoint joinPoint, EnclosingStaticPart enclosingStaticPart) {
System.out.println(joinPoint.getSignature() + " was invoked from "
+ enclosingStaticPart.getSignature());
}
}
测试代码的输出将是:
void q35991663.com.example.ExampleClass.something() 是从 void q35991663.com.example.ExampleTest.shouldTestSomething() 调用的
我还重写了您的examplePointCut
。切入点表达式 @annotation(com.example.Example)
的意思是
any join point where the subject has an annotation of type
com.example.Example
其中将包括 execution()
和 call()
类型连接点。在这种情况下,我们只需要 call()
连接点,因此如果您不打算将注释的值绑定(bind)到,则甚至不需要 @annotation()
建议上下文。
关于java - AspectJ 控制流/多个连接点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35991663/
我的问题与 Spring 的 AspectJ 模式有关,特别是如何启用它: 交易管理 缓存 1)我注意到,为了启用 AspectJ 模式进行事务管理,我只需执行以下操作: @Configuration
当我尝试使用 Java 17 运行 AspectJ 检测时,我总是会遇到如下错误: java.lang.reflect.InvocationTargetException at jav
我有一个应该记录的跟踪方面: 进入 退出(返回类型为 void) 返回[返回对象] 抛出[异常消息] 我对第二个有问题。我如何在不重复记录所有退出的情况下为这种情况创建建议,就像现在我有一个 @Aft
我已经使用 @Aspect 注释声明了我的切面,但建议似乎没有得到应用。该方面适用于我拥有的其他一些项目,主要区别似乎是其他项目完全使用注释连接,并且这个特定项目是 xml 连接的。唯一连接注释的 b
我正在尝试使用加载时编织将 perf4j 绑定(bind)到程序中,但它似乎在我的类路径中找不到 aop.xml。要么是这样,要么它没有编织这个方面,因为它没有找到它。我已启用 aop.xml 的详细
我是 spring 框架的新手,正在尝试一些示例来理解 AOP,这是我到目前为止所做的,但它不起作用。 问题是我一添加 对于 spring.xml,我的构建失败说无法创建具有空指针异常的 bean。但
我尝试使用 AspectJ 围绕 Kotlin 函数编织方面,但没有成功。也许我只是配置不正确,或者 AspectJ 不支持这个。 有谁知道这是否可以使用例如 maven 和 Eclipse(或 In
我正在使用 Eclipse 4 和 AspectJ 的最新版本进行开发。我正在用修改后的库(二进制编织)替换 Java 6 库。问题是当前正在编织的代码是 Java 7 代码,而我需要它是 Java
我正在将我的项目从 java 7 迁移到 java 8,我遇到的问题与使用 aspectj-maven-plugin 的 aspectj 编织有关。 我可以根据 Haus documentation
嘿,我想将 AOP 添加到我的 Web 项目中。我下载了 eclipse 3.4.1 的 ajdt2.0.1。但是当我将此项目转换为 AspectJ 项目时,出现了很多不应该发生的编译错误。比如“XX
我最近在我的 Windows 7 机器上从 eclipse Juno 升级到 Luna,我的 aspectj 编译出现问题。我收到此错误: [ERROR] Failed to execute goal
我想创建一个注释,它使用环绕方面来使用该注释清理参数。 例如,一个方法可能如下所示: public void setName(@Scrubbed String name) { ... } 也许 pub
我无法理解aspectJ的编译时和加载时编织,也无法弄清楚使用什么(以及如何使用ajc)来编译和构建我的项目。 这是我的项目结构:- TestProject:一个java服务库。这正被其他一些人使
我想拦截给定类的任何子类的非注释方法的执行。 例如,假设我有类 Base: public class Base { public void baseMethod() { //shouldn't
我正在尝试使用 AspectJ 和运行时编织。我创建了一个方面 @Aspect(value = "TraceAspect") public class TraceAspect { @Arou
我只是像下面描述的那样实现了AspectJ:https://stackoverflow.com/a/10998044/2182503 此解决方案工作正常,直到我注意到@Autowired中的@Init
我正在使用 AspectJ 来建议所有具有所选类参数的公共(public)方法。我尝试了以下方法: pointcut permissionCheckMethods(Session sess) :
我正在尝试创建一个 AspectJ Aspect 来拦截具有通用接口(interface)的返回方法。 这是我的 AspectJ @AspectJ public class MyAspect {
使用 Aspect annotation 创建方面时如所述 here ,是否可以将此注释与包含状态的类一起使用(例如,一旦命中切入点,成员变量就会发生变化)?或者换句话说:方面类是单例吗?注释的源代码
当我尝试使用 Roo 创建的 JPA 对象时出现此错误。 Entity manager has not been injected (is the Spring Aspects JAR configu
我是一名优秀的程序员,十分优秀!