【发布时间】: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