我能够从IntelliJ 2017.3启动Spring Boot Kotlin应用程序。在上次 IntelliJ 修复更新后,我无法从 IDE 启动该应用程序,从而出现以下异常:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: @Configuration class 'AccessConfig' may not be final
我可以像往常一样从终端启动它:java -jar xxxx.jar
这没有任何意义,因为我在我的 Gradle 配置中使用了必要的 Kotlin Spring 插件:
buildscript {
ext {
kotlinVersion = '1.2.21'
springBootVersion = '2.0.0.RC1'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.3'
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.5"
}
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven'
...
sourceCompatibility = 1.8
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
repositories {
mavenLocal()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://repo.maven.apache.org/maven2" }
maven { url 'https://jitpack.io' }
}
ext {
springCloudVersion = 'Finchley.M5'
mmaReleaseTrainVersion = 'Callao-SNAPSHOT'
junitVersion = '5.0.2'
}
有什么想法吗?
更新:
一种更简单的重现方法,只需使用 IntelliJ 的 Spring 初始值设定项创建一个 Spring Boot 项目,您将看到相同的结果:
演示应用:
@SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
SpringApplication.run(DemoApplication::class.java, *args)
}
错误:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: @Configuration class 'DemoApplication' may not be final. Remove the final modifier to continue.
Offending resource: com.example.demo.DemoApplication
build.gradle:
buildscript {
ext {
kotlinVersion = '1.2.10'
springBootVersion = '1.5.10.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
}
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:${kotlinVersion}")
compile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
testCompile('org.springframework.boot:spring-boot-starter-test')
}
首先,这都是由于类 Kotlin 类定义:
类的
open
注解与Java的final
相反:它允许其他人从这个类继承。默认情况下,Kotlin 中的所有类都是最终的
因此,如果你可以自由修改你的源代码,你可以让你的类不是最终的,只需在它的签名中添加open
,如下所示:
@SpringBootApplication
open class DemoApplication
fun main(args: Array<String>) {
SpringApplication.run(DemoApplication::class.java, *args)
}
或根据本文可能的解决方案之一:
@SpringBootApplication
是一个方便的注解,用@Configuration
、@EnableAutoConfiguration
和@ComponentScan
注解来标记类。强制使用 open 关键字的是强制使用 open 关键字的@Configuration
注释。
是尝试删除@SpringBootApplication
注释并使用@EnableAutoConfiguration
和@ComponentScan
来注释您的类来解决此问题。
修复了将 IntelliJ 的 Kotlin 插件升级到 1.2.21 的问题:https://plugins.jetbrains.com/plugin/6954-kotlin/update/42501
如果您在 IntelliJ 上收到此错误,即使使用 kotlin-spring 插件,您可能也需要检查是否已在 IntelliJ 中启用注释处理。
如果插件更新没有帮助,请检查 gradle 中的 kotlin 版本是否与 ide 的 kotlin 版本匹配。
这为我解决了它(只需将以下行添加到插件中(:
plugins {
id "org.jetbrains.kotlin.plugin.spring" version "1.5.10"
}
或者这种方法:
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
}
}
apply plugin: "kotlin-spring"
或者在Maven中:
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
对于非弹簧应用程序,请执行以下操作:
plugins {
id "org.jetbrains.kotlin.plugin.allopen" version "1.5.10"
}
或者这种方法:
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
}
}
apply plugin: "kotlin-allopen"
之后,指定将使类打开的注释列表
allOpen {
annotation("com.my.Annotation")
}
或者使用元注释:
@com.my.Annotation
annotation class MyFrameworkAnnotation
@MyFrameworkAnnotation
class MyClass //all-open
如何使用 Maven 的全开放:
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<configuration>
<compilerPlugins>
<plugin>all-open</plugin>
</compilerPlugins>
<pluginOptions>
<option>all-open:annotation=com.my.Annotation</option>
<option>all-open:annotation=com.their.AnotherAnnotation</option>
</pluginOptions>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
如果您需要将打开行为默认为 kotlin 中所有与 Spring 相关的类,您可以使用 从这里kotlin-allopen
插件
https://search.maven.org/classic/#search%7Cga%7C1%7Ckotlin%20allopen
更多信息 :https://www.baeldung.com/kotlin-allopen-spring