SpringBoot项目启动报错踩坑

不管现实多么惨不忍睹,都要持之以恒地相信,这只是黎明前短暂的黑暗而已。不要惶恐眼前的难关迈不过去,不要担心此刻的付出没有回报,别再花时间等待天降好运。真诚做人,努力做事!你想要的,岁月都会给你。SpringBoot项目启动报错踩坑,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

一、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循环依赖

***************************
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 又循环依赖了estimateServiceImpl ,这在代码层面是可以的,但在逻辑上是不允许的。

2.1、方法1

最简单粗暴的方法是在全局配置文件中允许循环引用存在,此属性默认值为false,显示声明为true,可回避项目启动时控制台循环引用异常。

spring.main.allow-circular-references=true
2.2、方法2

spring的核心是控制反转依赖注入,循环依赖就是在依赖注入这一步造成的,也就是说AB相互依赖的时候,初始化A必须要初始化B,初始化B必须也要初始化A,所以就会有死循环。

Spring2.6之前的版本会自动处理循环依赖,通过提前暴露bean的注入方式,将实例化和初始化分开做,2.6之后的版本不会自动处理了。

那如果业务场景实在需要循环依赖调用,有一个优雅的方式:控制反转,我们把控制权转到自己手上,使用方法的返回值获取实例对象,替换调通过成员变量注入实例对象,等我们用到的时候再去获取bean实例,不在初始化的时候注入,这样就优雅的避免了项目初始化的时候循环依赖导致的死循环。

示例如下:

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.

出现这个的原因一般是因为项目需要打印的环境变量太长,超过了限制,需要你缩短命令行来解决问题。

4.1、方法1

修改运行配置Configurations,将默认的Shorten command line的值user-local default 改为 JAR mainifest 或者 classpath file,这种办法每次需要对每个类单独设置。
在这里插入图片描述

4.2、方法2

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

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

在内部最下面添加一行

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

这种方式一次设置就行。
在这里插入图片描述


在这里插入图片描述

一起学编程,让生活更随和!如果你觉得是个同道中人,欢迎关注博主gzh:【随和的皮蛋桑】。专注于Java基础、进阶、面试以及计算机基础知识分享🐳。偶尔认知思考、日常水文🐌。

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/189372.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!