Cannot initialize context because there is already a root application context present

一、問題

Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!

二、問題描述

在自定義spring-boot的啓動監聽器時報錯,意思是說有多個內容加載監聽器,導致項目無法啓動。

三、問題解決

原錯誤代碼:

/**
 * 項目啓動監聽器
 */
@Component
public class StartListener extends ContextLoaderListener implements ApplicationRunner {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        super.contextInitialized(event);
    }

    @Override
    public void run(ApplicationArguments args) {

    }
}

原因就是在重載contextInitialized()方法時,調用了父類的初始化方法;去掉 super.contextInitialized(event);即可。

發佈了32 篇原創文章 · 獲贊 10 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章