springboot+kotlin启动时报错@Configuration class may not be final


好久没写简书了,中间经历了公司解散,重新面试,和等待通知
去研究了一段时间的区块链
现在重新回来啦

springboot + kotlin 启动报错

项目是通过spring initializr创建的 ,语言选择的kotlin,选择了web配置

生成的application如下:

package com.example.demo

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication
class DemoApplication

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

配置文件太长就没必要粘贴上来了

这个时候我们按以前的方式启动这个demo

出现以下异常:


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

大家特别注意这一句:

@Configuration class 'DemoApplication' may not be final.

回头看下我们的DemoApplication没毛病啊 spring initializr 帮我们生成的呀.

这是由于kotlin中所有类和方法默认都是final的,不能直接继承或重写

那如何解决呢?

需要继承的类或类中要重写的方法都应当在定义时添加open关键字。
我们去修改DemoApplication代码,在class前面加上open

open class DemoApplication

重新启动,问题搞定

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。