My project consists of a spring boot project and multiple extension packages (which can be understood as plugin).

Each extension package has its own spring application context and can use MVC related functions (such as defining controllers, interceptors, etc.), the extension package loader will load the Jar package through a custom URLCLassLoader and read the handlerMethods and interceptors in RequestMappingHandlerMapping and register them in the main springboot RequestMappingHandlerMapping.

When the main springboot program receives a request call and is scheduled through the DispatcherServlet, it will call the handler method registered with the extension package. At this time, the thread context classloader does not switch to the classLoader where the extension package is located.

At this point, the code implemented within the method may experience a ClassNotFoundException.

Comment From: bclozel

Thanks for the contribution, but I don't think we can change the current class loader for all Spring applications. Most applications will not use this plugin mechanism and we can't inflict such a change on all.

Maybe try to hook your plugin using a HandlerInterceptor?

Comment From: graceyu-own

Thank you for your suggestion, but the applicationContext of my extension packages does not have a servlet context.
I attempted to rewrite the createInvocableHandlerMethod of RequestMappingHandlerAdapter in main springboot applicationContext, it can solve my problem.

Spring Switching thread context classloader during mvc method execution