Spring 启动 webflux 应用程序不会启动 IllegalStateException
Spring boot webflux application won't start IllegalStateException
这是我的build.gradle
buildscript {
ext {
springBootVersion = '2.0.0.M2'
}
repositories {
mavenCentral()
jcenter()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-security',
'org.springframework.boot:spring-boot-starter-mail',
'org.springframework.boot:spring-boot-starter-validation',
'org.springframework.boot:spring-boot-starter-webflux',
// support libs
'commons-io:commons-io:2.5',
'commons-codec:commons-codec:1.10',
'org.apache.commons:commons-lang3:3.4',
'org.apache.commons:commons-collections4:4.1',
//validation
'javax.validation:validation-api:1.1.0.Final'
compileOnly 'org.springframework.boot:spring-boot-configuration-processor',
'org.projectlombok:lombok'
testCompile 'org.springframework.boot:spring-boot-starter-test'
}
当我尝试 运行 应用程序时,它失败了 java.lang.IllegalStateException: java.lang.NullPointerException
堆栈跟踪:
java.lang.IllegalStateException: java.lang.NullPointerException
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.stopAndReleaseReactiveWebServer(ReactiveWebServerApplicationContext.java:152) ~[spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:52) ~[spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
at com.getcredit.bankscraper.Application.main(Application.java:10) [main/:na]
Caused by: java.lang.NullPointerException: null
at org.springframework.boot.web.embedded.netty.NettyWebServer.stop(NettyWebServer.java:113) ~[spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.stopAndReleaseReactiveWebServer(ReactiveWebServerApplicationContext.java:148) ~[spring-boot-2.0.0.M2.jar:2.0.0.M2]
... 7 common frames omitted
项目是通过 start.spring.io 站点生成的。
经过一些测试,我发现了我的问题。那是我用@PostConstruct 注释的 bean 的 init 方法中的 NPE 异常。您可以像这样重现此问题:
@Component
class Test {
String str;
@PostConstruct
private void init() {
System.out.println(str.length());
}
}
我不明白为什么 spring 抛出如此不清楚的异常。
这是我的build.gradle
buildscript {
ext {
springBootVersion = '2.0.0.M2'
}
repositories {
mavenCentral()
jcenter()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-security',
'org.springframework.boot:spring-boot-starter-mail',
'org.springframework.boot:spring-boot-starter-validation',
'org.springframework.boot:spring-boot-starter-webflux',
// support libs
'commons-io:commons-io:2.5',
'commons-codec:commons-codec:1.10',
'org.apache.commons:commons-lang3:3.4',
'org.apache.commons:commons-collections4:4.1',
//validation
'javax.validation:validation-api:1.1.0.Final'
compileOnly 'org.springframework.boot:spring-boot-configuration-processor',
'org.projectlombok:lombok'
testCompile 'org.springframework.boot:spring-boot-starter-test'
}
当我尝试 运行 应用程序时,它失败了 java.lang.IllegalStateException: java.lang.NullPointerException
堆栈跟踪:
java.lang.IllegalStateException: java.lang.NullPointerException
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.stopAndReleaseReactiveWebServer(ReactiveWebServerApplicationContext.java:152) ~[spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:52) ~[spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
at com.getcredit.bankscraper.Application.main(Application.java:10) [main/:na]
Caused by: java.lang.NullPointerException: null
at org.springframework.boot.web.embedded.netty.NettyWebServer.stop(NettyWebServer.java:113) ~[spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.stopAndReleaseReactiveWebServer(ReactiveWebServerApplicationContext.java:148) ~[spring-boot-2.0.0.M2.jar:2.0.0.M2]
... 7 common frames omitted
项目是通过 start.spring.io 站点生成的。
经过一些测试,我发现了我的问题。那是我用@PostConstruct 注释的 bean 的 init 方法中的 NPE 异常。您可以像这样重现此问题:
@Component
class Test {
String str;
@PostConstruct
private void init() {
System.out.println(str.length());
}
}
我不明白为什么 spring 抛出如此不清楚的异常。