【问题标题】:Used kotlin-spring plugin, still getting class not open error使用了 kotlin-spring 插件,仍然出现类未打开错误
【发布时间】:2017-11-04 05:53:20
【问题描述】:

尽管添加了 kotlin-spring 插件,但我得到的课程不能是最终的,需要打开。该插件的全部目的是不手动将 open 关键字添加到每个类。

请指导我让 Kotling-Spring 插件与下面的代码一起工作。

build.gradle

buildscript {
    ext.kotlin_version = "1.1.2"

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
    }
}

apply plugin: "kotlin"
apply plugin: "kotlin-spring"
apply plugin: "kotlin-noarg"
apply plugin: "idea"

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
    compile"org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    compile "org.springframework:spring-context:4.3.8.RELEASE"
    testCompile "org.springframework:spring-test:4.3.8.RELEASE"
    testCompile "junit:junit:4.11"
}

AppConfig.java

@Configuration
class AppConfig {

    @Bean
    fun game(): Game {
        return BaseballGame(redSox(),cubs())
    }

    @Bean
    fun redSox(): Team {
        return RedSox()
    }

    @Bean
    fun cubs(): Team {
        return Cubs()
    }
}

错误:

Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: @Configuration class 'AppConfig' may not be final. Remove the final modifier to continue.
Offending resource: AppConfig

参考号:https://kotlinlang.org/docs/reference/using-gradle.html#plugin-and-versions

【问题讨论】:

    标签: spring gradle kotlin


    【解决方案1】:

    我在运行 Springboot Application 时也遇到了类似的问题。最后我了解到 Kotlin Class 是 final by Default 。您需要在类名之前添加 open

    所以,你需要改变类修饰符如下:

    打开类 AppConfig {

    参考:

    http://engineering.pivotal.io/post/spring-boot-application-with-kotlin/

    【讨论】:

      【解决方案2】:

      有类似的问题。无法从 IDE 运行,但 gradlew.bat build bootRun 工作。通过将 Kotlin 插件 in IDEA 从 1.2.40 更新到 1.2.41 解决。

      【讨论】:

        【解决方案3】:

        遇到了同样的问题。在 Intellij 中启用注释处理解决了这个问题:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-01-01
          • 1970-01-01
          • 2016-03-01
          • 2021-03-01
          • 2016-07-07
          • 2019-04-29
          • 2021-09-21
          • 2016-03-21
          相关资源
          最近更新 更多