【问题标题】:AspectJ - Weaving with custom ClassLoader at runtimeAspectJ - 在运行时使用自定义 ClassLoader 进行编织
【发布时间】:2017-03-29 06:49:22
【问题描述】:

我正在尝试在运行时加载类,并在此时将它们与一些 AspectJ 方面结合起来。我启用了加载时编织,当我更常规地使用它时它会起作用。

我的@Aspect 课程中有以下内容:

@Before("call(* mypackage.MyInterface.*())")
public void myInterfaceExecuteCall(JoinPoint thisJoinPoint,
        JoinPoint.StaticPart thisJoinPointStaticPart,
        JoinPoint.EnclosingStaticPart thisEnclosingJoinPointStaticPart) {
    System.out.println(thisJoinPoint.getSignature().getDeclaringType());
    System.out.println(thisJoinPoint.getSignature().getName());
}

然后我正在扫描 jar 并查找作为 MyInterface 实现的类:

URLClassLoader classLoader = new URLClassLoader(new URL[] { urlOfJar },
        ClassLoader.getSystemClassLoader());
WeavingURLClassLoader weaver = new WeavingURLClassLoader(
        classLoader);
HashSet<Class<?>> executableClasses = new HashSet<Class<?>>();
for (String name : classNamesInJar) {
    try {
        Class<?> myImplementation = weaver.loadClass(name);
        if (MyInterface.class.isAssignableFrom(myImplementation)) {
            executableClasses.add(myImplementation);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } catch (NoClassDefFoundError e) {
        e.printStackTrace();
    }
}

...然后我在某个时刻在加载的类中执行特定方法:

try {
    Method execute = myImplementation.getMethod("execute");
    execute.invoke(myImplementation.newInstance());
} catch (Exception e) {
    e.printStackTrace();
}

但是,当我调用execute.invoke(...) 时,我上面给你的@Before 方法永远不会执行(尽管execute 方法本身显然已执行,因为我看到了它的输出)。

有人知道我做错了什么吗?在调用加载的类的方法之前,有什么方法可以让 myInterfaceExecuteCall 被调用?

【问题讨论】:

    标签: java classloader aspectj


    【解决方案1】:

    好的,我发现了它是什么,但它并不能完全按照我的预期工作,但这里有一个解决方法:

    只需使用@Before("execution(* mypackage.MyInterface.*())") 而不是call。即使该类是由自定义类加载器在运行时手动加载的,这仍然有效。这是因为 AspectJ 不关心使用 Method.invoke(...) 完成的调用。我希望其他人可以使用此解决方法。

    这里是包含重要信息的文档的链接:

    例如,调用切入点不会挑选出对 java.lang.reflect.Method.invoke(Object, Object[]) 中实现的方法的反射调用。

    http://www.eclipse.org/aspectj/doc/released/progguide/implementation.html

    如果您有不同的解决方案,请不要犹豫回答!

    【讨论】:

    • 嗯,你最初打算这样做的方式不是 AspectJ 设计的正确方式。 call()execution() 切入点之间存在根本区别。我今天早些时候在stackoverflow.com/questions/17343092/… 谈到了它,也许它有助于为您澄清更多差异。
    【解决方案2】:

    如果我没记错的话,AspectJ 不能编织 JDK 类。这可以解释这一点:

    例如,调用切入点不会挑选出对 java.lang.reflect.Method.invoke(Object, Object[]) 中实现的方法的反射调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多