Why Spring AOP is not weaving external jars at runtime?

前端 未结 6 935
醉话见心
醉话见心 2020-12-29 14:46

I have a java application build upon Spring 3. This project has another jar as a dependency.

This dependency contains a @org.aspectj.lang.annotation.Aspect

6条回答
  •  长发绾君心
    2020-12-29 15:19

    Considering it works perfectly fine when the classes are packaged with the application and spring I can only think it would be a classloading issue.

    If it works fine when your bundled in your app then then when AOP scans all the classes that it will have to monitor then it is referencing the right classloader with all the right jars. But now when you remove it and set it into a JAR it is scanning under the classloader with all of the other third party jars.

    I am not 100% sure how it is mapped out but it could be something like this:

    Bootstrap Classloader <- Third Party Classloader  <- Application Class Loader (with all your classes)
                                  \                         \
                                   aspectj.jar               spring.jar
    

    If its aspect.jar is scanning under only its classloader then it will not be able to see 'all your classes'. One way you can try confirming this is to get a heap dump of your app. Run it against Eclipse MAT, Check out Class Loader explorer and look for the aspect classes. If they do not reside under the same classloader as your application you would have to look at a way to have tomcat tell the third party libraries of the application classes.

提交回复
热议问题