- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想记录类列表(可能属于不同的包)中所有方法的条目。请注意,这些方法应该只属于指定的类。
我试过以下方法,但这些都不起作用
(1) 使用 if() 切入点这里报错
"incompatible number of arguments to pointcut, expected 1 found 0"
@Pointcut("execution(*.*(..)) && if()")
public static boolean mycut(JoinPoint jp) {
boolean matches = ... ;//Test using jp if the declaring class belongs to the list
return matches;
}
(2) 切入点与aop.xml结合使用这里报错
java.lang.NoSuchMethodError:
com.mypackage.TraceAspect.aspectOf()Lcom/df/jc/aspect/TraceAspect;
//in com.mypackage.TraceAspect aspect class
@Pointcut("execution(*.*(..)) && !within(com.mypackage.TraceAspect)")
public void mycut(){
}
//in aop.xml
<weaver>
<include within="package1.Class1">
<include within="package2.Class2">
<include within="package3.Class3">
</weaver>
这里出了什么问题?
当然可以通过在切入点中单独指定每个类来完成,但这对于数百个类来说是不可扩展的。理想情况下,如果可以从外部文本文件中获取类列表(以便于配置),那就太好了
最佳答案
至于你最后的评论:除了糟糕的设计,我并没有劝阻你做任何事情,我只是想鼓励你做正确的事:重构,不要让自己的生活变得比必要的更艰难。您甚至不了解 AspectJ 语法基础知识,但您已经想要实现具有大量类的过于复杂的场景,这是维护的噩梦。我试图通过激励你不要做出短视的决定来提供帮助。相信我,多年来我一直在使用 AspectJ 进行所谓的具有大量遗留代码的真实项目。避免即使是最便宜的重构也比进行智能重构要昂贵得多——不是太多,但根据童子军规则就足够了:让营地比你发现时更干净。这是有返回的,相信我。
无论如何,谈谈你的代码片段:
execution(*.*(..))
在语法上是错误的,因为您没有为要匹配的方法指定返回类型(或它的占位符)。您想使用 execution(* *.*(..))
或简写版本 execution(* *(..))
。@Before("mycut()")
的内容,但正确的应该是 @Before("mycut(jp)")
。话虽如此,这里是一个简单、完全独立且可编译的示例:
司机申请:
package de.scrum_master.app;
public class Application {
public static void main(String[] args) {
System.out.println(multiply(3, add(4, 5)));
}
public static int multiply(int i, int j) { return i * j; }
public static int add(int i, int j) { return i + j; }
}
看点:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class TraceAspect {
@Pointcut("execution(* *(..)) && if()")
public static boolean hasMatchingSignature(JoinPoint thisJoinPoint) {
return !thisJoinPoint.getSignature().getName().equals("main");
}
@Before("hasMatchingSignature(thisJoinPoint)")
public void myAdvice(JoinPoint thisJoinPoint) {
System.out.println(thisJoinPoint);
}
}
示例输出:
execution(int de.scrum_master.app.Application.add(int, int))
execution(int de.scrum_master.app.Application.multiply(int, int))
27
如果您的 if()
切入点只返回 true
,输出也会显示 main
的执行。
关于aop - 类列表的所有方法上的 AspectJ 切入点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23527469/
我的问题与 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
我是一名优秀的程序员,十分优秀!