IntelliJ Idea 2017.3无法启动Kotlin Spring Boot App – @Configuration类可能不是最终的
我能够从IntelliJ 2017.3推出一个春季启动Kotlin应用程序。 在最后一次IntelliJ修复更新后,我无法从IDE启动该应用程序,得到此exception:
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项目,您将看到相同的结果:
DemoApplication:
@SpringBootApplication class DemoApplication fun main(args: Array) { 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 class级的定义 :
一个类的
open
注释与Java的final
一个是相反的:它允许其他人从这个类inheritance。 默认情况下,Kotlin中的所有类都是最终的
所以如果你可以自由修改你的源代码,你可以让你的课程不是最终的,只需要添加open
的签名,如下所示:
@SpringBootApplication open class DemoApplication fun main(args: Array) { SpringApplication.run(DemoApplication::class.java, *args) } }
或根据这篇文章的可能的解决方案之一:
@SpringBootApplication
是一个便利的注释,它使用@Configuration
@EnableAutoConfiguration
,@ComponentScan
@EnableAutoConfiguration
和@ComponentScan
注释标记类。 这是强制使用open关键字的@Configuration
注释。
是试图删除@SpringBootApplication
批注和@EnableAutoConfiguration
和@ComponentScan
注解你的类来解决这个问题。
修正了将IntelliJ的Kotlin插件升级到1.2.21: https ://plugins.jetbrains.com/plugin/6954-kotlin/update/42501
- 分割所有输出目录gradle
- 使用特定软件包时无法找到或加载主类
- Gradle同步失败:无法GET'https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-gradle-plugin/1.1.3-2/kotlin-gradle-plugin-1.1.3-2.pom'
- Groovy类在gradle中不可用
- 在kotlin中为Gradle脚本配置IntelliJ自动完成
- 如何在多模块gradle项目中提供测试数据和类
- Android Studio 3.0 Canary 1:Gradle同步错误
- 在kotlin REPL中导入罐子
- 如何用Kotlin和Tornado FX设置Gluon应用程序