Hin*_*ton 5 java aspectj classloader
我正在尝试在运行时加载类,并在此时将它们与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());
}
Run Code Online (Sandbox Code Playgroud)
然后,我在扫描jars并查找作为以下实现的类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();
}
}
Run Code Online (Sandbox Code Playgroud)
...然后在某个时刻在加载的类中执行特定的方法:
try {
Method execute = myImplementation.getMethod("execute");
execute.invoke(myImplementation.newInstance());
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
但是,我在上面给您提供的@Before方法永远不会在我调用时执行execute.invoke(...)(尽管该execute方法本身显然已经执行了,因为我看到了它的输出)。
有人知道我在做什么错吗?在myInterfaceExecuteCall调用已加载的类的方法之前,如何进行调用?
好的,我发现它是什么,并且它并不能完全按照我的预期工作,但是这里有一个解决方法:
只是做一个@Before("execution(* mypackage.MyInterface.*())")代替call。即使该类是由自定义类加载器手动加载的,也可以在运行时加载。这是因为AspectJ不在乎使用using进行的调用Method.invoke(...)。我希望其他人可以使用此替代方法。
这是指向包含难忘信息的文档的链接:
例如,调用切入点不会选择对java.lang.reflect.Method.invoke(Object,Object [])中实现的方法的反射调用。
http://www.eclipse.org/aspectj/doc/released/progguide/implementation.html
如果您有其他解决方案,请随时回答!
| 归档时间: |
|
| 查看次数: |
2708 次 |
| 最近记录: |