Order of class loading from a .war file

前端 未结 6 803
南方客
南方客 2020-12-09 10:45

I\'ve got a question regarding the guarantees, if any, in the following scenario (note that the question is not \"How to do this in a different way?\", the question

6条回答
  •  无人及你
    2020-12-09 11:17

    Both the WEB-INF/lib/*.jar files and the WEB-INF/classes directory are in the same ClassLoader. It would be as if you started an application with all the jars listed in the ClassPath. As a class name needs to be resolved, the ClassLoader will find the first class that matches from its resources. The exact order it searches in is nondeterministic--it depends on the platform.

    Java Packages were designed to address the problem of name clashes such as what you described. It is never a good idea to deliberately name a class the same as what is bundled in its own jar file. The better solution would be to extend the class and use the new version in your code. If you need to alter functionality of that class, then you might look into the black magic of Java Aspect Oriented Programming.

提交回复
热议问题