spring-projects / spring-boot

Spring Boot helps you to create Spring-powered, production-grade applications and services with absolute minimum fuss.
https://spring.io/projects/spring-boot
Apache License 2.0
75.26k stars 40.7k forks source link

A web app with `com.amazonaws:DynamoDBLocal` on the classpath fails to start #4089

Closed wilkinsona closed 9 years ago

wilkinsona commented 9 years ago
java.lang.ClassNotFoundException: org.eclipse.jetty.webapp.WebAppContext
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_60]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_60]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_60]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_60]
    ... 31 common frames omitted
Wrapped by: java.lang.NoClassDefFoundError: org/eclipse/jetty/webapp/WebAppContext
    at org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$EmbeddedJetty.jettyEmbeddedServletContainerFactory(EmbeddedServletContainerAutoConfiguration.java:91) ~[classes/:na]
    at org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$EmbeddedJetty$$EnhancerBySpringCGLIB$$aa08d6ae.CGLIB$jettyEmbeddedServletContainerFactory$0(<generated>) ~[spring-core-4.2.2.BUILD-SNAPSHOT.jar:na]
    at org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$EmbeddedJetty$$EnhancerBySpringCGLIB$$aa08d6ae$$FastClassBySpringCGLIB$$3a66e1cc.invoke(<generated>) ~[spring-core-4.2.2.BUILD-SNAPSHOT.jar:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:318) ~[spring-context-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]
    at org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$EmbeddedJetty$$EnhancerBySpringCGLIB$$aa08d6ae.jettyEmbeddedServletContainerFactory(<generated>) ~[spring-core-4.2.2.BUILD-SNAPSHOT.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]
    ... 20 common frames omitted
Wrapped by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory]: Factory method 'jettyEmbeddedServletContainerFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: org/eclipse/jetty/webapp/WebAppContext
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]
    ... 19 common frames omitted
Wrapped by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jettyEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedJetty.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory]: Factory method 'jettyEmbeddedServletContainerFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: org/eclipse/jetty/webapp/WebAppContext
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) ~[spring-beans-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) ~[spring-beans-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305) ~[spring-beans-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) ~[spring-beans-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:201) ~[spring-beans-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:193) [classes/:na]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:156) [classes/:na]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) [classes/:na]
    ... 8 common frames omitted
Wrapped by: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jettyEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedJetty.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory]: Factory method 'jettyEmbeddedServletContainerFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: org/eclipse/jetty/webapp/WebAppContext
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) [classes/:na]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) [classes/:na]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:720) [classes/:na]
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:352) [classes/:na]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:292) [classes/:na]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1033) [classes/:na]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1022) [classes/:na]
    at sample.freemarker.SampleWebFreeMarkerApplication.main(SampleWebFreeMarkerApplication.java:26) [classes/:na]

LocalDynamoDB depends on some of Jetty, but not all of the bits that Boot needs. We need to tighten up the classpath conditions.

A workaround is to add a @Bean that forces the use of Tomcat:

@Bean
public EmbeddedServletContainerFactory tomcatContainerFactory() {
    return new TomcatEmbeddedServletContainerFactory();
}

Stack Overflow question: http://stackoverflow.com/questions/32862876/springboot-and-dynamodb-local-embedded/32921693#32921693

brianou7 commented 4 years ago

Hi @wilkinsona, first of all thanks for this solution, it has been of great help for me. And, how about this issue using Spring Webflux and Netty?

I've had the same issue, and temporally I did this to solved it:

@Bean
public NettyReactiveWebServerFactory getNettyReactiveWebServerFactory() {
    return new NettyReactiveWebServerFactory();
}

But, it think that it's a similar issue.

wilkinsona commented 4 years ago

Thanks very much for letting us know, @brianou7. That is indeed a similar issue. I've opened https://github.com/spring-projects/spring-boot/issues/22275 so that we can fix it.

shapan1 commented 3 years ago
@Bean
public NettyReactiveWebServerFactory getNettyReactiveWebServerFactory() {
    return new NettyReactiveWebServerFactory();
}

I am new to working within the bowels of Spring-webflux, however do I need to add this bean in the test (that uses the DynamoDBLocal Embedded DB)?

wilkinsona commented 3 years ago

@shapan1 No, that shouldn't be necessary due to the fix made for https://github.com/spring-projects/spring-boot/issues/22275. If you believe it is necessary and would like us to take a closer look, please open a new issue with a minimal sample that reproduces the problem.