参考:https://blog.csdn.net/riyunzhu/article/details/63259718
jdk1.8 springboot替换容器在网上搜索只需要两步
如果不是可能就会报错Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
手动注入Jetty容器工厂
解决方法:
方法一:降低springboot版本到1.3.8,缺点:1.3.8和1.5.x配置升级很大不同,降低版本兼容不合适
方法二:升级jdk1.8,考虑目前开发环境都是1.7,缺点也不合适
方法三:手动指定jetty版本为jdk1.7版本
jdk1.8 springboot替换容器在网上搜索只需要两步
<!-- web剔除tomcat容器= -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入Jetty容器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
如果你是jdk1.8版本
启动正常!
如果不是可能就会报错Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
2017-03-18 17:03:23.212 ERROR 11488 --- [main] o.s.boot.SpringApplication : Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
报错的原因是EmbeddedServletContainerAutoConfiguration没有自动加载Jetty容器
但是ConditionOnClass的类都满足,没有办法只有手动注入一个EmbeddedServletContainerFactory对应Jetty的实现
@Configuration
@ConditionalOnClass({ Servlet.class, Server.class, Loader.class,
WebAppContext.class })
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
public static class EmbeddedJetty {
@Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
return new JettyEmbeddedServletContainerFactory();
}
}
手动注入Jetty容器工厂
启动
package com.liuhao.study.config;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author: liuhao
* @Date: 2018/10/31 13:58
* @Description:
**/
@Configuration
public class JettyConfiguration {
@Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
return new JettyEmbeddedServletContainerFactory();
}
}
继续报错
Caused 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.UnsupportedClassVersionError: org/eclipse/jetty/webapp/WebAppContext : Unsupported major.minor version 52.0
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
... 19 common frames omitted
Caused by: java.lang.UnsupportedClassVersionError: org/eclipse/jetty/webapp/WebAppContext : Unsupported major.minor version 52.0
原因:
Caused by: java.lang.UnsupportedClassVersionError: org/eclipse/jetty/webapp/WebAppContext : Unsupported major.minor version 52.0这个错误是一个典型的jdk版本不匹配导致的错误
解决方法:
知道了问题是因为jetty版本和jdk版本不匹配不一致导致的就很好解决了
springboot使用的版本是1.5.10.RELEASE,jetty的版本是9.4.8.v20171121,需要jdk1.8环境
方法一:降低springboot版本到1.3.8,缺点:1.3.8和1.5.x配置升级很大不同,降低版本兼容不合适
方法二:升级jdk1.8,考虑目前开发环境都是1.7,缺点也不合适
方法三:手动指定jetty版本为jdk1.7版本
<jetty.version>9.2.12.v20150709</jetty.version>
sprinboot-整合jetty jdk1.7项目地址: https://github.com/harryLiu92/springboot-study/tree/master/springboot-jetty