1. 程式人生 > 其它 >IDEA中SpringBoot啟動無法載入主類

IDEA中SpringBoot啟動無法載入主類

問題描述:

一、不小心將啟動類下的檔案刪除了,當再次恢復後,啟動時報錯如下

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-05-06 21:27:18.275 ERROR 10968 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active)

碰到這個問題,首先想到的是資料庫配置是不是有問題,檢視application.properties檔案配置正常

因為當時只修改了依賴和工程目錄,所以猜想可能是引入了不必要的pom依賴,於是刪除了DataSource相關的依賴

問題依舊存在

二、這時又想到可能是配置檔案沒有生效,於是將application.properties檔案中的內容全部註釋掉,再次啟動發現還是同樣的問題,從而將問題定位到了application.properties檔案沒有生效

於是又去查詢此檔案不生效的原因

大致原因有以下幾種:

1、在pom中新增配置

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.*</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.*</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

2、將packaging中的pom修改為jar

<packaging>jar</packaging>

嘗試完上面兩種方法後,問題依舊存在

於是重新整理思路,想想自己改了什麼東西,想起來做了工程的複製和刪除操作,於是又想到是不是啟動類需要手動配置一下

於是修改啟動類,重新啟動,好了,這裡出現了更嚴重的問題

三、IDEA中SpringBoot啟動錯誤無法載入主類

至此,已經由一個問題引發了3個問題,深感乏力,對於工程目錄還是最好不要動

那麼拋開其他問題,來解決無法載入主類的問題

查了一下無法載入主類主要有以下原因:

1、所需類沒有編譯或者通過maven運行了clean

mvn clean compile,將專案重新編譯

mvn install,打包

mvn spring-boot:run,啟動專案

問題依舊沒有解決

後來偶然發現了一個部落格

找到了解決問題的方法

重點來了:

專案裡面.idea檔案 刪除
重啟idea
mvn claean install

做了上述操作後,發現問題終於解決了,上面的所有問題都沒有了,專案啟動正常

四、總結


以上所有的問題都是由於自己修改了工程目錄引起的

引發了一系列的問題,所以涉及工程目錄的時候,不要輕易更改

對於問題的解決,一定是層層遞進,有幾個排查的方向,針對每種猜想,去找部落格檢視解決思路,然後在理解部落格解決思路的前提下,去嘗試解決問題

將答案一個個排除,避免在同一種方法上浪費過多的時間