最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot項目啟動報錯踩坑實戰(zhàn)記錄

 更新時間:2023年02月08日 08:57:39   作者:一宿君  
這篇文章主要給大家介紹了關(guān)于SpringBoot項目啟動報錯踩坑的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

一、redis和jedis版本不匹配

報錯日志如下:

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版本不匹配導致的,項目中引入redis默認版本為2.7.0

<!-- spring redis session 默認2.7.0 -->
<dependency>
     <groupId>org.springframework.session</groupId>
     <artifactId>spring-session-data-redis</artifactId>
</dependency>

通過https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis查看對應jedis版本應該為3.8.0,而項目中是3.0.0,修改為3.8.0即可

<dependency>
     <groupId>redis.clients</groupId>
     <artifactId>jedis</artifactId>
     <version>3.8.0</version>
</dependency>

二、spring循環(huán)依賴

***************************
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 依賴了fillDataAlarmServiceImpl ,fillDataAlarmServiceImpl 又循環(huán)依賴了estimateServiceImpl ,這在代碼層面是可以的,但在邏輯上是不允許的。

2.1、方法1

最簡單粗暴的方法是在全局配置文件中允許循環(huán)引用存在,此屬性默認值為false,顯示聲明為true,可回避項目啟動時控制臺循環(huán)引用異常。

spring.main.allow-circular-references=true

2.2、方法2

spring的核心是控制反轉(zhuǎn)依賴注入,循環(huán)依賴就是在依賴注入這一步造成的,也就是說AB相互依賴的時候,初始化A必須要初始化B,初始化B必須也要初始化A,所以就會有死循環(huán)。

Spring2.6之前的版本會自動處理循環(huán)依賴,通過提前暴露出bean的注入方式,將實例化和初始化分開做,2.6之后的版本不會自動處理了。

那如果業(yè)務場景實在需要循環(huán)依賴調(diào)用,有一個優(yōu)雅的方式:控制反轉(zhuǎn),我們把控制權(quán)轉(zhuǎn)到自己手上,使用方法的返回值獲取實例對象,替換調(diào)通過成員變量注入實例對象,等我們用到的時候再去獲取bean實例,不在初始化的時候注入,這樣就優(yōu)雅的避免了項目初始化的時候循環(huán)依賴導致的死循環(huán)。

示例如下:

A依賴B

@Service
@RequiredArgsConstructor
public class AServiceImpl implements AService {

    private final ConfigurableListableBeanFactory beanFactory;

    @Override
    public BService getBService() {
        return beanFactory.getBean(BService.class);
    }
}

B依賴A

@Service
@RequiredArgsConstructor
public class BServiceImpl implements BService {

    private final ConfigurableListableBeanFactory beanFactory;

    @Override
    public AService getAService() {
        return beanFactory.getBean(AService.class);
    }

}

三、允許DefaultServlet默認注冊

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不再注冊,如果應用程序需要要它,需要進行一定的配置。

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運行報錯

項目debug報錯如下:

Error running ‘MallTest.testRun’: Command line is too long. Shorten command line for MallTest.testRun.

出現(xiàn)這個的原因一般是因為項目需要打印的環(huán)境變量太長,超過了限制,需要你縮短命令行來解決問題。

4.1、方法1

修改運行配置Configurations,將默認的Shorten command line的值user-local default 改為 JAR mainifest 或者 classpath file,這種辦法每次需要對每個類單獨設(shè)置。

4.2、方法2

想一步到位,在項目的.idea/workspace.xml文件中添加配置,找到

<component name="PropertiesComponent"></component>

在內(nèi)部最下面添加一行

<property name="dynamic.classpath" value="true" />

這種方式一次設(shè)置就行。

總結(jié)

到此這篇關(guān)于SpringBoot項目啟動報錯踩坑的文章就介紹到這了,更多相關(guān)SpringBoot項目啟動報錯內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

固始县| 札达县| 肇源县| 堆龙德庆县| 绵阳市| 天峨县| 大名县| 南丰县| 三台县| 新乡市| 怀化市| 肥东县| 象山县| 奉化市| 彰化市| 白银市| 永清县| 图木舒克市| 甘谷县| 双桥区| 沅陵县| 马边| 鲁甸县| 贺兰县| 湟源县| 化州市| 甘孜| 沂南县| 富蕴县| 河池市| 彭阳县| 鄂伦春自治旗| 灯塔市| 甘德县| 吕梁市| 庆城县| 西乌珠穆沁旗| 达州市| 观塘区| 宽甸| 明水县|