springboot+kotlin啟動(dòng)時(shí)報(bào)錯(cuò)@Configuration class may not be final


好久沒寫簡(jiǎn)書了,中間經(jīng)歷了公司解散,重新面試,和等待通知
去研究了一段時(shí)間的區(qū)塊鏈
現(xiàn)在重新回來(lái)啦

springboot + kotlin 啟動(dòng)報(bào)錯(cuò)

項(xiàng)目是通過(guò)spring initializr創(chuàng)建的 ,語(yǔ)言選擇的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)
}

配置文件太長(zhǎng)就沒必要粘貼上來(lái)了

這個(gè)時(shí)候我們按以前的方式啟動(dòng)這個(gè)demo

出現(xiàn)以下異常:


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中所有類和方法默認(rèn)都是final的,不能直接繼承或重寫

那如何解決呢?

需要繼承的類或類中要重寫的方法都應(yīng)當(dāng)在定義時(shí)添加open關(guān)鍵字。
我們?nèi)バ薷腄emoApplication代碼,在class前面加上open

open class DemoApplication

重新啟動(dòng),問(wèn)題搞定

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容