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
————————————————
版權聲明:本文為CSDN博主「熏肉大餅」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/w5167839/article/details/83585970