AOP中需要通过反射获取方法参数名称,所以用到javassist,部署后报异常:javassist.NotFoundException: xxx.xxx.xxxServiceImpl
war包中配的AOP拦截jar包中的Service方法,jar已通过maven引入到war中。
官方文档是这样说的:
The default ClassPool returned by a static method ClassPool.getDefault() searches the same path that the underlying JVM (Java virtual machine) has. If a program is running on a web application server such as JBoss and Tomcat, the ClassPoolobject may not be able to find user classes since such a web application server uses multiple class loaders as well as the system class loader. In that case, an additional class path must be registered to the ClassPool.
翻译:
由静态方法ClassPool.getDefault()返回的默认的ClassPool搜索与底层JVM(Java虚拟机)相同的路径。如果一个程序在像JBoss和Tomcat这样的web应用程序服务器上运行,那么ClassPoolobject可能无法找到用户类,因为这样的web应用程序服务器使用了多个类加载器和系统类装入器。在这种情况下,必须向ClassPool注册一个额外的类路径。
说实话,我不是太理解这个意思,能否有人能清楚的解释一下,或者给我一个思考的方向,比如:你需要懂得哪些知识,才能更好的理解这段话。谢谢
类加载的知识,jvm类加载器,web容器(tomcat,jetty)架构设计下的类加载器,类加载模式。
因为tomcat和jboss使用的是独立的classloader(架构的原因,必须实现自己的类加载器),而Javassist是通过默认的classloader(JVM)加载类,因此直接对tomcat context中定义的类做toClass会抛出ClassCastException异常,可以用tomcat的classloader加载字节码。
第二点,同一个类,如果由两个不同的父加载器加载,那么他们是不相同的两个类