SpringBoot项目启动报错踩坑

一、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基础、进阶、面试以及计算机基础知识分享🐳。偶尔认知思考、日常水文🐌。

在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一宿君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值