Workaround Class Not Found Exception: Tomcat Embedded Servlet Container. Spring Boot 2 Metrics

Full exception text:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatMetricsBinder' defined in class path resource [io/micrometer/spring/autoconfigure/web/tomcat/TomcatMetricsAutoConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [io.micrometer.spring.web.tomcat.TomcatMetricsBinder] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader@7adf9f5f]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:579)
	...
Caused by: java.lang.IllegalStateException: Failed to introspect Class [io.micrometer.spring.web.tomcat.TomcatMetricsBinder] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader@7adf9f5f]
	at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481)
	...
Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer
	at java.lang.Class.getDeclaredMethods0(Native Method)
	...
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	...

Most likely, this error occurs due to the old metrics that came from Spring Boot 1 used together with Spring Boot 2.

If you urgently need to start the application, then as a workaround, you can disable one of the metrics modules in this way: find the place in your application where you use the @EnableAutoConfiguration annotation, and add:

@EnableAutoConfiguration(exclude = {
    TomcatMetricsAutoConfiguration.class
})

Telegram channel

If you still have any questions, feel free to ask me in the comments under this article or write me at promark33@gmail.com.

If I saved your day, you can support me 🤝