【问题标题】:Spring Boot bootRun on Java 6Java 6 上的 Spring Boot 启动运行
【发布时间】:2018-04-22 07:29:29
【问题描述】:

我在使用 Spring Boot 1.5.8 上的 Jetty 自动配置器时遇到问题。我需要使用 Jetty 8 而不是 Jetty 9 来兼容 Java 6,但是自动配置器没有检测到 jetty 类:

EmbeddedServletContainerAutoConfiguration.EmbeddedJetty: 不匹配: - @ConditionalOnClass 没有找到所需的类 '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:无法启动嵌入式容器;嵌套异常是 org.springframework.context.ApplicationContextException:由于缺少 EmbeddedServletContainerFactory bean,无法启动 EmbeddedWebApplicationContext。 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) 在 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
    }
}

【问题讨论】:

  • 哪个弹簧靴版本。还有为什么要复杂添加码头?只需设置正确的版本。设置ext['jetty.version'] = '8.1.22.v20160922',而不是你现在拥有的。这同样适用于杰克逊ext['jackson.version] = '2.7.9'`。
  • @M.Deinum 这是 1.5.8。我这样做是因为我不知道更好。
  • Spring Boot 1.5 默认需要 Java 7,你需要为 Java 6 做额外的配置,这在 the reference guide 中有很好的解释。
  • @M.Deinum 将依赖部分更改为使用ext 就足以让自动配置器工作(尽管我不确定为什么)。如果你想把它写下来作为答案,我会接受。

标签: java spring-boot


【解决方案1】:

默认运行 Spring Boot 1.5 requires Java 7 或更高版本。但是,可以在 Java 6 上运行 Spring Boot 1.5,这在 the reference guide 中有很好的记录。

在您的情况下,您想得太多了,就像指定所需的 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 将包含正确的版本,您不需要自己包含它们。

【讨论】:

  • 参考指南的链接是什么?现在好像老了
  • 链接到 Spring Boot 1.5 文档而不是当前文档(Spring Boot 1.5 是最后一个支持 Java 6,Spring Boot 2 需要 Java 8)。
猜你喜欢
  • 1970-01-01
  • 2015-02-08
  • 2017-01-07
  • 1970-01-01
  • 2020-03-01
  • 1970-01-01
  • 2017-09-28
  • 1970-01-01
相关资源
最近更新 更多