SpringBoot項(xiàng)目啟動(dòng)報(bào)錯(cuò)踩坑實(shí)戰(zhàn)記錄
一、redis和jedis版本不匹配
報(bào)錯(cuò)日志如下:
Caused by: java.lang.ClassNotFoundException: redis.clients.jedis.DefaultJedisClientConfig at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:418) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ... 127 common frames omitted
原因就是SpringBoot和jedis版本不匹配導(dǎo)致的,項(xiàng)目中引入redis默認(rèn)版本為2.7.0
<!-- spring redis session 默認(rèn)2.7.0 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
通過(guò)https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis查看對(duì)應(yīng)jedis版本應(yīng)該為3.8.0,而項(xiàng)目中是3.0.0,修改為3.8.0即可
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.8.0</version>
</dependency>
二、spring循環(huán)依賴(lài)
***************************
APPLICATION FAILED TO START
***************************Description:
The dependencies of some of the beans in the application context form a cycle:
projectRelatedController (field private cn......EstimateServiceImpl cn......ProjectRelatedController.estimateService)
┌─────┐
| estimateServiceImpl (field cn...FillDataAlarmService cn......fillDataAlarmService)
↑ ↓
| fillDataAlarmServiceImpl (field cn......EstimateServiceImpl cn......FillDataAlarmServiceImpl.estimateService)
└─────┘Action:
Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
Disconnected from the target VM, address: '127.0.0.1:1499', transport: 'socket'
Process finished with exit code 1
可以看到estimateServiceImpl 依賴(lài)了fillDataAlarmServiceImpl ,fillDataAlarmServiceImpl 又循環(huán)依賴(lài)了estimateServiceImpl ,這在代碼層面是可以的,但在邏輯上是不允許的。
2.1、方法1
最簡(jiǎn)單粗暴的方法是在全局配置文件中允許循環(huán)引用存在,此屬性默認(rèn)值為false,顯示聲明為true,可回避項(xiàng)目啟動(dòng)時(shí)控制臺(tái)循環(huán)引用異常。
spring.main.allow-circular-references=true
2.2、方法2
spring的核心是控制反轉(zhuǎn)和依賴(lài)注入,循環(huán)依賴(lài)就是在依賴(lài)注入這一步造成的,也就是說(shuō)AB相互依賴(lài)的時(shí)候,初始化A必須要初始化B,初始化B必須也要初始化A,所以就會(huì)有死循環(huán)。
Spring2.6之前的版本會(huì)自動(dòng)處理循環(huán)依賴(lài),通過(guò)提前暴露出bean的注入方式,將實(shí)例化和初始化分開(kāi)做,2.6之后的版本不會(huì)自動(dòng)處理了。
那如果業(yè)務(wù)場(chǎng)景實(shí)在需要循環(huán)依賴(lài)調(diào)用,有一個(gè)優(yōu)雅的方式:控制反轉(zhuǎn),我們把控制權(quán)轉(zhuǎn)到自己手上,使用方法的返回值獲取實(shí)例對(duì)象,替換調(diào)通過(guò)成員變量注入實(shí)例對(duì)象,等我們用到的時(shí)候再去獲取bean實(shí)例,不在初始化的時(shí)候注入,這樣就優(yōu)雅的避免了項(xiàng)目初始化的時(shí)候循環(huán)依賴(lài)導(dǎo)致的死循環(huán)。
示例如下:
A依賴(lài)B
@Service
@RequiredArgsConstructor
public class AServiceImpl implements AService {
private final ConfigurableListableBeanFactory beanFactory;
@Override
public BService getBService() {
return beanFactory.getBean(BService.class);
}
}
B依賴(lài)A
@Service
@RequiredArgsConstructor
public class BServiceImpl implements BService {
private final ConfigurableListableBeanFactory beanFactory;
@Override
public AService getAService() {
return beanFactory.getBean(AService.class);
}
}
三、允許DefaultServlet默認(rèn)注冊(cè)
Caused by: java.lang.IllegalStateException: Unable to locate the default servlet for serving static content. Please set the 'defaultServletName' property explicitly. at org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler.setServletContext(DefaultServletHttpRequestHandler.java:111) at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.enable(DefaultServletHandlerConfigurer.java:85) at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.enable(DefaultServletHandlerConfigurer.java:71) at cn.sto.financial.estimate.interceptor.WebMvcConfig.configureDefaultServletHandling(WebMvcConfig.java:44) at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:644) at Disconnected from the target VM, address: '127.0.0.1:8711', transport: 'socket' Process finished with exit code 1
Spring嵌入式Servlet容器提供的DefaultServlet不再注冊(cè),如果應(yīng)用程序需要要它,需要進(jìn)行一定的配置。
3.1、方法1
server.servlet.register-default-servlet=true
3.2、方法2
@SpringBootApplication
public class StarterApplication {
@Bean
WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> enableDefaultServlet() {
return factory -> factory.setRegisterDefaultServlet(true);
}
public static void main(String[] args) {
SpringApplication.run(StarterApplication.class,args);
}
}
四、debug運(yùn)行報(bào)錯(cuò)
項(xiàng)目debug報(bào)錯(cuò)如下:
Error running ‘MallTest.testRun’: Command line is too long. Shorten command line for MallTest.testRun.
出現(xiàn)這個(gè)的原因一般是因?yàn)轫?xiàng)目需要打印的環(huán)境變量太長(zhǎng),超過(guò)了限制,需要你縮短命令行來(lái)解決問(wèn)題。
4.1、方法1
修改運(yùn)行配置Configurations,將默認(rèn)的Shorten command line的值user-local default 改為 JAR mainifest 或者 classpath file,這種辦法每次需要對(duì)每個(gè)類(lèi)單獨(dú)設(shè)置。

4.2、方法2
想一步到位,在項(xiàng)目的.idea/workspace.xml文件中添加配置,找到
<component name="PropertiesComponent"></component>
在內(nèi)部最下面添加一行
<property name="dynamic.classpath" value="true" />
這種方式一次設(shè)置就行。

總結(jié)
到此這篇關(guān)于SpringBoot項(xiàng)目啟動(dòng)報(bào)錯(cuò)踩坑的文章就介紹到這了,更多相關(guān)SpringBoot項(xiàng)目啟動(dòng)報(bào)錯(cuò)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Java程序模擬實(shí)現(xiàn)新冠病毒傳染效果
這篇文章主要介紹了用Java程序模擬實(shí)現(xiàn)新冠病毒傳染效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
基于Spring?Boot的線(xiàn)程池監(jiān)控問(wèn)題及解決方案
這篇文章主要介紹了基于Spring?Boot的線(xiàn)程池監(jiān)控方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
spring hibernate實(shí)現(xiàn)動(dòng)態(tài)替換表名(分表)的方法
下面小編就為大家?guī)?lái)一篇spring hibernate實(shí)現(xiàn)動(dòng)態(tài)替換表名(分表)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
實(shí)例講解分布式緩存軟件Memcached的Java客戶(hù)端使用
這篇文章主要介紹了分布式緩存軟件Memcached的Java客戶(hù)端使用,Memcached在GitHub上開(kāi)源,作者用其Windows平臺(tái)下的版本進(jìn)行演示,需要的朋友可以參考下2016-01-01
Spring Boot 項(xiàng)目做性能監(jiān)控的操作流程
這篇文章主要介紹了Spring Boot 項(xiàng)目如何做性能監(jiān)控,本文通過(guò)實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
springboot中關(guān)于自動(dòng)建表,無(wú)法更新字段的問(wèn)題
這篇文章主要介紹了springboot中關(guān)于自動(dòng)建表,無(wú)法更新字段的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02

