在使用带有spring-boot的kotlin时,启用Hystrix会导致NullPointerException

问题描述 投票:2回答:1

当我将Hystrix支持添加到kotlin spring boot项目时,抽象类的某些字段突然变为null。似乎Hystrix代理不能正确处理字段:

我有一个抽象的kotlin类ClientHandler有两个字段loggertest直接初始化。我有一个SomeHandler的kotlin子类ClientHandler,我在课堂上通过自动装配使用。正如预期的那样,在使用子类时,会初始化两个字段。

但是当我添加Hystrix支持(取消注释@EnableHystrix@HystrixCommand)时,抽象类中的两个字段loggertest突然变为空。

Debugger

你知道为什么会这样吗? Spring为SomeHandler类添加了一个代理类,Hystrix做了同样的事情。所以我假设在与Kotlin结合的双重代理中出现了问题!?我对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 {

}

应用程序

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>
java spring spring-boot kotlin hystrix
1个回答
0
投票

我能够自己解决这个问题:函数send必须是开放的,因此Hystrix和Spring将在抽象类(包括两个字段)周围创建一个合适的代理。

© www.soinside.com 2019 - 2024. All rights reserved.