SpringBoot項目報錯:"Error starting ApplicationContext…."解決辦法

發現錯誤

SpringBoot項目報錯:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

以下方案80%可以幫助您解決這些個‘可惡的’問題

報錯內容和截圖如下:

Connected to the target VM, address: '127.0.0.1:4963', transport: 'socket'

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v2.3.10.RELEASE)

2021-12-25 14:52:41.578  INFO 13204 --- [           main] m.c.DiitMicrosoftserviceCheckApplication : Starting DiitMicrosoftserviceCheckApplication on 老雲的計算機 with PID 13204 (D:\svn\workspace_nxzrbhdzt\Gh_yizhangtu\dit-check\target\classes started by liuyundashen in D:\svn\workspace_nxzrbhdzt\Gh_yizhangtu\dit-check)
2021-12-25 14:52:41.583  INFO 13204 --- [           main] m.c.DiitMicrosoftserviceCheckApplication : No active profile set, falling back to default profiles: default
2021-12-25 14:52:43.485  INFO 13204 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-12-25 14:52:43.503  INFO 13204 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-12-25 14:52:43.504  INFO 13204 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.45]
2021-12-25 14:52:43.668  INFO 13204 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-12-25 14:52:43.668  INFO 13204 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1979 ms
2021-12-25 14:52:43.832  INFO 13204 --- [           main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2021-12-25 14:52:43.910 ERROR 13204 --- [           main] com.alibaba.druid.pool.DruidDataSource   : testWhileIdle is true, validationQuery not set
2021-12-25 14:52:43.917  INFO 13204 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
 _ _   |_  _ _|_. ___ _ |    _ 
| | |\/|_)(_| | |_\  |_)||_|_\ 
     /               |         
                        3.2.0 
2021-12-25 14:52:44.365  WARN 13204 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cheakRecordController': Unsatisfied dependency expressed through field 'recordService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'TRecordServiceImpl': Unsatisfied dependency expressed through field 'packageService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resultPackageBaseinfoServiceImpl': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'importDB.datasource.url' in value "${importDB.datasource.url}"
2021-12-25 14:52:44.366  INFO 13204 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} closing ...
2021-12-25 14:52:44.368  INFO 13204 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} closed
2021-12-25 14:52:44.372  INFO 13204 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2021-12-25 14:52:44.391  INFO 13204 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-12-25 14:52:44.410 ERROR 13204 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cheakRecordController': Unsatisfied dependency expressed through field 'recordService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'TRecordServiceImpl': Unsatisfied dependency expressed through field 'packageService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resultPackageBaseinfoServiceImpl': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'importDB.datasource.url' in value "${importDB.datasource.url}"
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1425) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.14.RELEASE.jar:5.2.14.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.14.RELEASE.jar:5.2.14.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.10.RELEASE.jar:2.3.10.RELEASE]

——在看到這些個問題的時候,日志基本都會告訴你錯誤的地方,所以我們大部分可以根據日志報錯的信息分析錯誤的原因:

一、編譯出問題

仔細看報出的問題,有沒有如:“Description:…” 這樣的字眼,沒有 “描述”,如上圖所示,這種情況下的操作:
1.clean清理一下; 2.然後install重新編譯一下 ;

3.也可以用這個命令,重新編譯一下

這個時候重新啟動,如果還是報同樣的錯誤,打開如下圖位置,看是否有編譯的文件,如mapper.xml文件,application.yml或者application.properties文件,以及一些配置文件等

——哎!生氣,這裡發現真的沒有mapper和配置文件,隻有main下面的.jar文件,該怎麼辦呢?

——pom.xml文件中給加入要編譯的文件位置依賴,如下:(這是我的文件位置,你根據你的進行調整)

<!--引入本地資源-->
        <resources>
            <!--加載lib文件,特殊情況下會有lib,但大部分都靠依賴下載瞭-->
            <resource>
                <directory>lib</directory>
                <targetPath>BOOT-INF/lib/</targetPath>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
            <!--打jar包-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <!--將properties和xml文件編譯-->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

好的,清理——>打包——>啟動——>運行成功,搞定收工

二、請求接口重復

根據日志看出它說路徑問題;這樣想你寫的接口名字是否重復,隻有一個斜杠或者沒斜杠的情況

如下截圖:

修改如下:

好的,重新啟動,搞定收工

三、加@Mapper註解

這些情況就是看描述的問題

如圖:

這種情況,看是不是沒有掃描到它需要的東西,給加上@Mapper註解(是掃描dao層的),或者 加上@Service註解(掃描service層)即可。

四、加@SpringBootApplication註解,數據庫問題

根據問題看出:springboot啟動時會自動註入數據源和配置jpa ,解決:

在@SpringBootApplication中排除其註入

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})

五、端口重復問題

查看是否自己的運行端口沖突,將其kill殺死,重新啟動,或者跟換端口

以下是查看端口沖突的方式:

window鍵+R,輸入“netstat -aon|findstr 8080”找到占用8080端口的進程號:

2.輸入“tasklist|findstr “3412” ”:

查看進程號為“3412”的進程:

3.輸入“taskkill /f /t /im QQ.exe”:(/f:強制性的結束進程)

結束這個進程

現在可以重新啟動一下,看是否運行成功

六、包沖突

還有的情況可能是包沖突瞭,可以將maven倉庫清理一下自己的包,重新下載一下

總結

到此這篇關於SpringBoot項目報錯:"Error starting ApplicationContext…."解決辦法的文章就介紹到這瞭,更多相關SpringBoot項目報錯內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: