在 spring-boot 中使用 kotlin 时启用 Hystrix 会导致 NullPointerException
Enabling Hystrix leads to NullPointerException when using kotlin with spring-boot
当我将 Hystrix 支持添加到 kotlin spring 引导项目时,抽象 class 的某些字段突然为空。似乎 Hystrix 代理没有正确处理字段:
我有一个抽象的 kotlin class ClientHandler
有两个字段 logger
和 test
直接初始化。我有一个 ClientHandler
的 kotlin subclass SomeHandler
,我通过自动装配在 class 中使用它。正如预期的那样,当使用 subclass 时,两个字段被初始化。
但是当我添加 Hystrix 支持(取消注释 @EnableHystrix
和 @HystrixCommand
)时,摘要 class 中的两个字段 logger
和 test
突然为空.
你知道为什么会这样吗? Spring 将代理 class 添加到 SomeHandler
class,Hystrix 做同样的事情。所以我假设在与 Kotlin 结合的双重代理中出了问题!?我对 java class 做了同样的事情,一切都很顺利。
package com.test
import mu.KotlinLogging
import org.springframework.stereotype.Component
import java.lang.RuntimeException
abstract class ClientHandler {
protected val logger = KotlinLogging.logger {}
private val test = "test"
//@HystrixCommand(commandKey = "Some Service")
fun send(communication: Communication): ResponseObject {
try {
return callApi(communication)
} catch (e: Exception) {
logger.warn("Could not send information", e)
return ResponseObject()
}
}
protected abstract fun callApi(communication: Communication): ResponseObject
}
@Component
class SomeHandler : ClientHandler() {
override fun callApi(communication: Communication): ResponseObject {
throw RuntimeException()
}
}
class Communication {
}
class ResponseObject {
}
申请
package com.test
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
//@EnableHystrix
@SpringBootApplication
class SomeApplication {
fun main(args: Array<String>) {
runApplication<SomeApplication>(*args)
}
}
测试
package com.test
import org.hibernate.validator.internal.util.Contracts.assertNotNull
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner
@RunWith(SpringRunner::class)
@SpringBootTest
class CustomerApiImplTest {
@Autowired
private lateinit var someHandler: SomeHandler
@Test
fun testIt() {
val communication = Communication()
val responseEntity = someHandler.send(communication)
assertNotNull(responseEntity)
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<properties>
<kotlin.version>1.3.21</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>io.github.microutils</groupId>
<artifactId>kotlin-logging</artifactId>
<version>1.6.25</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>target/generated-sources/swagger/src/gen/java/main</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
<args>
<arg>-Xjsr305=strict</arg> <!-- Enable strict mode for JSR-305 annotations -->
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我自己弄明白了:函数 send
必须打开,所以 Hystrix 和 Spring 将围绕抽象 class 创建一个适当的代理,包括两个字段。
当我将 Hystrix 支持添加到 kotlin spring 引导项目时,抽象 class 的某些字段突然为空。似乎 Hystrix 代理没有正确处理字段:
我有一个抽象的 kotlin class ClientHandler
有两个字段 logger
和 test
直接初始化。我有一个 ClientHandler
的 kotlin subclass SomeHandler
,我通过自动装配在 class 中使用它。正如预期的那样,当使用 subclass 时,两个字段被初始化。
但是当我添加 Hystrix 支持(取消注释 @EnableHystrix
和 @HystrixCommand
)时,摘要 class 中的两个字段 logger
和 test
突然为空.
你知道为什么会这样吗? Spring 将代理 class 添加到 SomeHandler
class,Hystrix 做同样的事情。所以我假设在与 Kotlin 结合的双重代理中出了问题!?我对 java class 做了同样的事情,一切都很顺利。
package com.test
import mu.KotlinLogging
import org.springframework.stereotype.Component
import java.lang.RuntimeException
abstract class ClientHandler {
protected val logger = KotlinLogging.logger {}
private val test = "test"
//@HystrixCommand(commandKey = "Some Service")
fun send(communication: Communication): ResponseObject {
try {
return callApi(communication)
} catch (e: Exception) {
logger.warn("Could not send information", e)
return ResponseObject()
}
}
protected abstract fun callApi(communication: Communication): ResponseObject
}
@Component
class SomeHandler : ClientHandler() {
override fun callApi(communication: Communication): ResponseObject {
throw RuntimeException()
}
}
class Communication {
}
class ResponseObject {
}
申请
package com.test
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
//@EnableHystrix
@SpringBootApplication
class SomeApplication {
fun main(args: Array<String>) {
runApplication<SomeApplication>(*args)
}
}
测试
package com.test
import org.hibernate.validator.internal.util.Contracts.assertNotNull
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner
@RunWith(SpringRunner::class)
@SpringBootTest
class CustomerApiImplTest {
@Autowired
private lateinit var someHandler: SomeHandler
@Test
fun testIt() {
val communication = Communication()
val responseEntity = someHandler.send(communication)
assertNotNull(responseEntity)
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<properties>
<kotlin.version>1.3.21</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>io.github.microutils</groupId>
<artifactId>kotlin-logging</artifactId>
<version>1.6.25</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>target/generated-sources/swagger/src/gen/java/main</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
<args>
<arg>-Xjsr305=strict</arg> <!-- Enable strict mode for JSR-305 annotations -->
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我自己弄明白了:函数 send
必须打开,所以 Hystrix 和 Spring 将围绕抽象 class 创建一个适当的代理,包括两个字段。