美文网首页
springboot+kotlin启动时报错@Configura

springboot+kotlin启动时报错@Configura

作者: _奔波儿灞_ | 来源:发表于2017-12-15 16:53 被阅读504次

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

    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
    

    重新启动,问题搞定

    相关文章

      网友评论

          本文标题:springboot+kotlin启动时报错@Configura

          本文链接:https://www.haomeiwen.com/subject/naydwxtx.html