【发布时间】: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