从 1.3.3 升级到 1.3.5 后 Spring Boot 应用程序无法启动。Spring 无法启动嵌入式容器(Tomcat 8)并显示以下错误消息:
org.springframework.context.ApplicationContextException:无法启动嵌入式容器;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“embeddedServletContainerFactory”的 bean 时出错:bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [wad.config.HttpsConfiguration$1]:未找到默认构造函数;嵌套异常是 java.lang.NoSuchMethodException: wad.config.HttpsConfiguration$1.()
基本上这个错误消息说它不能创建embeddedServletContainerFactory
,但我不清楚[wad.config.HttpsConfiguration$1]
指的是什么。Java 配置类本身在包中wad.config
并命名为HttpsConfiguration
. 我试图将空构造函数添加到我的HttpsConfiguration.java
,但它没有帮助。
以下是我的 POM 的相关部分:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
...
</dependencies>
我的应用程序类:
@EntityScan(
basePackageClasses = {Application.class, Jsr310JpaConverters.class}
)
@SpringBootApplication
@Import({DevProfile.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
这是我从 8080 -> 8443 端口重定向的配置(可通过 application.properties 配置):
@Configuration
public class HttpsConfiguration {
@Value("${server.port}")
private int httpsPort;
@Value("${server.port.http}")
private int httpPort;
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
return tomcat;
}
// redirect from (http) port to (https) if https is enabled.
private Connector initiateHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(httpPort);
connector.setSecure(false);
connector.setRedirectPort(httpsPort);
return connector;
}
application.properties 配置:
...
#Actuator port
management.port = 9001
#HTTPS port
server.port=8443
#HTTP port
server.port.http=8080
#Enable SSL
server.ssl.enabled=true
...
更新:
使用上述 POM、application.properties 和 HttpSecurity & Application 类配置的 Spring Boot 1.3.5 项目可以重现问题。