Skip to content

Set Tomcat's WebappClassLoader as the main thread's context classloader #2308

@wilkinsona

Description

@wilkinsona

When a web app is started on standalone Tomcat the WebappClassLoader is created and used to load all of the application's types. It's also the thread context class loader when the application's started. In a Boot app the uses embedded Tomcat things are a little different. The application's types are loaded on the main thread using Boot's class loader. Tomcat's then started and the WebappClassLoader is created. This class loader then isn't set as the context class loader when the application's started.

One problem that this causes is that it breaks JNDI lookups performed during startup. If you use postProcessContext to configure JNDI, this is done on one of Tomcat's startup threads. If you then attempt a JNDI lookup during application startup this lookup happens on the main thread. The lookup fails as there's nothing linking the main thread to the JNDI context. Setting the main thread's context class loader to be Tomcat's WebappClassLoader fixes this.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions