Spring Tool Suite:@Configuration 类可能不是最终的。删除最终修饰符以继续

ren*_*ena 6 eclipse kotlin

由于以下原因,我无法启动 spring boot kotlin 应用程序:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: @Configuration class 'TestApplication' may not be final. Remove the final modifier to continue.
Run Code Online (Sandbox Code Playgroud)

我知道 @configuration 和 @bean 类不能是最终的,所以我在我的 pom 文件中添加了 allopen 插件:

<build>
    <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <configuration>
                <args>
                    <arg>-Xjsr305=strict</arg>
                </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>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

如果我打包我的应用程序并运行它(java -jar)它可以工作,但是当我尝试从 STS 启动它时它不起作用。

Kotlin 版本:1.2.71 Kotlin eclipse 插件:0.8.13 STS:3.9.5

小智 5

这个问题可以重复:

IntelliJ Idea 2017.3 无法启动 Kotlin Spring Boot App - @Configuration 类可能不是最终的

或者

使用 kotlin-spring 插件,仍然得到 class not open 错误

. 无论如何,我刚刚找到了解决方案

您只需要向 TestApplication kotlin 类添加 open 修饰符

@SpringBootApplication
open class TestApplication 
Run Code Online (Sandbox Code Playgroud)

可能会解决您的问题。

  • 这不是 kotlin-maven-allopen 的用途吗? (3认同)