Java 6上的Spring Boot bootRun(Spring Boot bootRun on Java 6)

编程入门 行业动态 更新时间:2024-10-12 10:17:48
Java 6上的Spring Boot bootRun(Spring Boot bootRun on Java 6)

我在Spring Boot 1.5.8上使用Jetty的autoconfigurer时遇到了麻烦。 我需要使用Jetty 8而不是Jetty 9来兼容Java 6,但自动配置器不能检测码头类:

EmbeddedServletContainerAutoConfiguration.EmbeddedJetty: Did not match: - @ConditionalOnClass did not find required class 'org.eclipse.jetty.webapp.WebAppContext' (OnClassCondition)

我的build.gradle的依赖项部分:

dependencies { compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.12' compile 'org.springframework.boot:spring-boot-starter-web', { exclude module: 'spring-boot-starter-tomcat' exclude group: 'com.fasterxml.jackson.core' } compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.7.9' // last version for Java 6 compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.7.9' compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.7.9.1' providedCompile 'javax.servlet:javax.servlet-api:3.0.1' //providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' providedRuntime 'org.springframework.boot:spring-boot-starter-jetty', { exclude group: 'org.eclipse.jetty' exclude group: 'org.eclipse.jetty.websocket' } def JETTY8_VERSION = '8.1.22.v20160922' ['jetty-server', 'jetty-webapp', 'jetty-servlets', 'jetty-continuation', 'jetty-client', 'jetty-http', 'jetty-util', 'jetty-io', 'jetty-servlet', 'jetty-xml', 'jetty-security'].each { providedRuntime "org.eclipse.jetty:$it:$JETTY8_VERSION" } }

这随后导致:

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. at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ...

奇怪的是,如果我明确声明EmbeddedServletContainerFactoryBean ,它的工作原理是:

@Bean EmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() { try { def clazz = Class.forName('org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory') clazz.newInstance() } catch (NoClassDefFoundError cnfe) { null } }

I'm having trouble with the autoconfigurer for Jetty on Spring Boot 1.5.8. I need to use Jetty 8 instead of Jetty 9 for Java 6 compatibility, but the autoconfigurer does not detect the jetty classes:

EmbeddedServletContainerAutoConfiguration.EmbeddedJetty: Did not match: - @ConditionalOnClass did not find required class 'org.eclipse.jetty.webapp.WebAppContext' (OnClassCondition)

The dependencies part of my build.gradle:

dependencies { compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.12' compile 'org.springframework.boot:spring-boot-starter-web', { exclude module: 'spring-boot-starter-tomcat' exclude group: 'com.fasterxml.jackson.core' } compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.7.9' // last version for Java 6 compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.7.9' compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.7.9.1' providedCompile 'javax.servlet:javax.servlet-api:3.0.1' //providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' providedRuntime 'org.springframework.boot:spring-boot-starter-jetty', { exclude group: 'org.eclipse.jetty' exclude group: 'org.eclipse.jetty.websocket' } def JETTY8_VERSION = '8.1.22.v20160922' ['jetty-server', 'jetty-webapp', 'jetty-servlets', 'jetty-continuation', 'jetty-client', 'jetty-http', 'jetty-util', 'jetty-io', 'jetty-servlet', 'jetty-xml', 'jetty-security'].each { providedRuntime "org.eclipse.jetty:$it:$JETTY8_VERSION" } }

This subsequently results in:

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. at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ...

The strange part is, if I declare EmbeddedServletContainerFactoryBean explicitly, it works:

@Bean EmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() { try { def clazz = Class.forName('org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory') clazz.newInstance() } catch (NoClassDefFoundError cnfe) { null } }

最满意答案

默认情况下,Spring Boot 1.5 需要运行Java 7或更高版本。 但是,可以在Java 6上运行Spring Boot 1.5,这在参考指南中有很好的记录。

在你的情况下,你只是在指定所需的Jetty版本时过度思考。

ext['jetty.version'] = '8.1.22.v20160922' dependencies { compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.12' compile 'org.springframework.boot:spring-boot-starter-web', { exclude module: 'spring-boot-starter-tomcat' exclude group: 'com.fasterxml.jackson.core' } compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.7.9' // last version for Java 6 compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.7.9' compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.7.9.1' providedCompile 'javax.servlet:javax.servlet-api:3.0.1' providedRuntime 'org.springframework.boot:spring-boot-starter-jetty', { exclude group: 'org.eclipse.jetty.websocket' } }

这样Spring Boot将包含正确的版本,你不需要自己包含它们。

By default Spring Boot 1.5 requires Java 7 or higher to run. However it is possible to run Spring Boot 1.5 on Java 6 and that is quite nicely documented in the reference guide.

In your case you are overthink things it is as simply as specifying the desired Jetty version.

ext['jetty.version'] = '8.1.22.v20160922' dependencies { compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.12' compile 'org.springframework.boot:spring-boot-starter-web', { exclude module: 'spring-boot-starter-tomcat' exclude group: 'com.fasterxml.jackson.core' } compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.7.9' // last version for Java 6 compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.7.9' compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.7.9.1' providedCompile 'javax.servlet:javax.servlet-api:3.0.1' providedRuntime 'org.springframework.boot:spring-boot-starter-jetty', { exclude group: 'org.eclipse.jetty.websocket' } }

This way Spring Boot will include the proper versions and you don't need to include them yourself.

更多推荐

本文发布于:2023-07-17 15:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1146347.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Spring   Java   bootRun   Boot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!