我尝试从AspectJ Cookbook完成LTW示例,但它不起作用。 我创建并编译了Java类:
public class MyClass{
public void foo(int number, String name){
System.out.println("Inside of foo");
}
public static void main(String[] args) {
MyClass myObject = new MyClass();
myObject.foo(1, "Str");
}
}
它运作得很好:
c:\TEMP\examples>java MyClass
Inside of foo
然后我创建了方面:
public aspect HelloWorld{
pointcut callPointcut(): call(void MyClass.foo(int, String));
before() : callPointcut(){
System.out.println("Hello World from advice");
}
}
编译它:
c:\TEMP\examples>c:\tools\aspectj1.8\bin\ajc -outjar my.jar HelloWorld.aj
C:\TEMP\examples\HelloWorld.aj:4 [warning] no match for this type name: MyClass [Xlint:invalidAbsoluteTypeName]
pointcut callPointcut(): call(void MyClass.foo(int, String));
^^^^^^^^^
[Xlint:invalidAbsoluteTypeName]
1 warning
然后尝试运行:
c:\TEMP\examples>java -classpath "C:\tools\aspectj1.8\lib\aspectjweaver.jar" "-Djava.system.class.loader=org.aspectj.weaver.loadtime.WeavingURLClassLoader" -Daj.class.path=. "-Daj.aspect.path=c:\TEMP\examples" MyClass
java.lang.ExceptionInInitializerError
at org.aspectj.weaver.WeaverMessages.<clinit>(WeaverMessages.java:18)
at org.aspectj.weaver.bcel.ClassPathManager.addPath(ClassPathManager.java:81)
at org.aspectj.weaver.bcel.ClassPathManager.<init>(ClassPathManager.java:63)
at org.aspectj.weaver.bcel.BcelWorld.<init>(BcelWorld.java:285)
at org.aspectj.weaver.tools.WeavingAdaptor.init(WeavingAdaptor.java:176)
at org.aspectj.weaver.tools.WeavingAdaptor.<init>(WeavingAdaptor.java:109)
at org.aspectj.weaver.loadtime.WeavingURLClassLoader.<init>(WeavingURLClassLoader.java:75)
at org.aspectj.weaver.loadtime.WeavingURLClassLoader.<init>(WeavingURLClassLoader.java:52)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.SystemClassLoaderAction.run(Unknown Source)
at java.lang.SystemClassLoaderAction.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)
at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)
Caused by: java.lang.IllegalStateException: recursive invocation
at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)
at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)
at java.util.ServiceLoader.loadInstalled(Unknown Source)
at java.util.ResourceBundle.<clinit>(Unknown Source)
... 17 more
Error occurred during initialization of VM
java.lang.ExceptionInInitializerError
at org.aspectj.weaver.WeaverMessages.<clinit>(WeaverMessages.java:18)
at org.aspectj.weaver.bcel.ClassPathManager.addPath(ClassPathManager.java:81)
at org.aspectj.weaver.bcel.ClassPathManager.<init>(ClassPathManager.java:63)
at org.aspectj.weaver.bcel.BcelWorld.<init>(BcelWorld.java:285)
at org.aspectj.weaver.tools.WeavingAdaptor.init(WeavingAdaptor.java:176)
at org.aspectj.weaver.tools.WeavingAdaptor.<init>(WeavingAdaptor.java:109)
at org.aspectj.weaver.loadtime.WeavingURLClassLoader.<init>(WeavingURLClassLoader.java:75)
at org.aspectj.weaver.loadtime.WeavingURLClassLoader.<init>(WeavingURLClassLoader.java:52)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.SystemClassLoaderAction.run(Unknown Source)
at java.lang.SystemClassLoaderAction.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)
at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)
Caused by: java.lang.IllegalStateException: recursive invocation
at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)
at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)
at java.util.ServiceLoader.loadInstalled(Unknown Source)
at java.util.ResourceBundle.<clinit>(Unknown Source)
at org.aspectj.weaver.WeaverMessages.<clinit>(WeaverMessages.java:18)
at org.aspectj.weaver.bcel.ClassPathManager.addPath(ClassPathManager.java:81)
at org.aspectj.weaver.bcel.ClassPathManager.<init>(ClassPathManager.java:63)
at org.aspectj.weaver.bcel.BcelWorld.<init>(BcelWorld.java:285)
at org.aspectj.weaver.tools.WeavingAdaptor.init(WeavingAdaptor.java:176)
at org.aspectj.weaver.tools.WeavingAdaptor.<init>(WeavingAdaptor.java:109)
at org.aspectj.weaver.loadtime.WeavingURLClassLoader.<init>(WeavingURLClassLoader.java:75)
at org.aspectj.weaver.loadtime.WeavingURLClassLoader.<init>(WeavingURLClassLoader.java:52)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.SystemClassLoaderAction.run(Unknown Source)
at java.lang.SystemClassLoaderAction.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)
at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)
我尝试不同的解决方案,只有当我提供-Daj.aspect.path = c:\ TEMP \ examples时才会发生异常,但据我所知,这是因为weaver只会加载方面提供此选项。如果没有此选项,Java程序无需在加载时编织任何方面。
答案 0 :(得分:2)
你能使用javaagent吗?我觉得它比修改类加载器更简单:
javac MyClass.java
ajc HelloWorld.java -1.8 -outxml -d out
export CLASSPATH=.:out:$CLASSPATH
java -javaagent:$AJHOME/lib/aspectjweaver.jar MyClass
Hello World from advice
Inside of foo
ajc调用上的-outxml
选项会在META-INF/aop-ajc.xml
文件夹中生成out
文件,这就是代理打开编织所需的内容。 (该xml文件将简单列出要打开的方面以及可选的一些额外的weaver配置)