[英]Enabling Hystrix leads to NullPointerException when using kotlin with spring-boot
When I add Hystrix support to a kotlin spring boot project, some fields of an abstract class are suddenly null. 当我向kotlin spring boot项目添加Hystrix支持时,抽象类的某些字段突然为空。 It seems the Hystrix proxy does not handle fields properly: 似乎Hystrix代理无法正确处理字段:
I have an abstract kotlin class ClientHandler
having two fields logger
and test
directly initialized. 我有一个抽象的kotlin类ClientHandler
它具有两个字段logger
并直接初始化了test
。 I have a kotlin subclass SomeHandler
of ClientHandler
which I use via autowiring in a class. 我有一个ClientHandler
的kotlin子类SomeHandler
,可以通过在类中自动装配来使用。 As expected when using the subclass the two fields are initialized. 如预期的那样,使用子类时,将初始化两个字段。
But when I add Hystrix support (uncomment @EnableHystrix
and @HystrixCommand
) the two fields logger
and test
in the abstract class are suddenly null. 但是,当我添加Hystrix支持(取消注释@EnableHystrix
和@HystrixCommand
)时,抽象类中的两个字段logger
和test
突然@HystrixCommand
空。
Do you have any ideas why this happens? 您有任何想法为什么会这样吗? Spring adds a proxy class to the SomeHandler
class, Hystrix does the same. Spring将代理类添加到SomeHandler
类中,Hystrix也是一样。 So I assume something goes wrong in the double proxying in combination with Kotlin!? 因此,我认为与Kotlin结合使用双重代理时出了点问题! I did the same with a java class and everything went fine. 我对一个Java类做了同样的事情,一切都很好。
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 {
}
The application 应用程序
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)
}
}
The test 考试
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)
}
}
The pom.xml 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将围绕包含两个字段的抽象类创建一个适当的Proxy。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.