IntelliJ Idea 2017.3 unable to start Kotlin Spring Boot App - @Configuration class may not be final

后端 未结 5 638
悲&欢浪女
悲&欢浪女 2021-01-03 23:52

I was able to launch a Spring Boot Kotlin App from IntelliJ 2017.3. After the last IntelliJ fix update I cannot start that application from the IDE, getting this exception:<

相关标签:
5条回答
  • 2021-01-04 00:16

    First of all, it's all due to class Kotlin class definition:

    The open annotation on a class is the opposite of Java's final: it allows others to inherit from this class. By default, all classes in Kotlin are final

    so if you are free to modify your source code, you can make your class not final, just adding open to it's signature as follows:

    @SpringBootApplication
    open class DemoApplication
    
    fun main(args: Array<String>) {
        SpringApplication.run(DemoApplication::class.java, *args)
    }
    

    or one of the possible solutions, according to this article:

    The @SpringBootApplication is a convenience annotation that marks the class with the @Configuration, @EnableAutoConfiguration and @ComponentScan annotations. It is the @Configuration annotation that forces the use of the open keyword.

    Is to try to remove the @SpringBootApplication annotation and annotate your class with @EnableAutoConfiguration and @ComponentScan to solve this issue.

    0 讨论(0)
  • 2021-01-04 00:21

    Fixed upgrading IntelliJ's Kotlin pluging to 1.2.21: https://plugins.jetbrains.com/plugin/6954-kotlin/update/42501

    0 讨论(0)
  • 2021-01-04 00:21

    If you get this error on IntelliJ, even with kotlin-spring plugin, you may want to check if the annotation processing has been enabled in IntelliJ.

    0 讨论(0)
  • 2021-01-04 00:22

    If you need to have open behavior default to all Spring related classes in kotlin, you can use kotlin-allopen plugin from here

    https://search.maven.org/classic/#search%7Cga%7C1%7Ckotlin%20allopen

    More info : https://www.baeldung.com/kotlin-allopen-spring

    0 讨论(0)
  • 2021-01-04 00:26

    If plugin update does not help, check if version of kotlin in gradle matches ide's kotlin version.

    0 讨论(0)
提交回复
热议问题