报错日志如下:
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
<dependency>
<groupId>org.springframework.sessiongroupId>
<artifactId>spring-session-data-redisartifactId>
dependency>
通过https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis查看对应jedis版本应该为3.8.0,而项目中是3.0.0,修改为3.8.0即可
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>3.8.0version>
dependency>
***************************
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
,这在代码层面是可以的,但在逻辑上是不允许的。
最简单粗暴的方法是在全局配置文件中允许循环引用存在,此属性默认值为false,显示声明为true,可回避项目启动时控制台循环引用异常。
spring.main.allow-circular-references=true
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);
}
}
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不再注册,如果应用程序需要要它,需要进行一定的配置。
server.servlet.register-default-servlet=true
@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报错如下:
Error running ‘MallTest.testRun’: Command line is too long. Shorten command line for MallTest.testRun.
出现这个的原因一般是因为项目需要打印的环境变量太长,超过了限制,需要你缩短命令行来解决问题。
修改运行配置Configurations,将默认的Shorten command line的值user-local default 改为 JAR mainifest 或者 classpath file,这种办法每次需要对每个类单独设置。
想一步到位,在项目的.idea/workspace.xml文件中添加配置,找到
<component name="PropertiesComponent">component>
在内部最下面添加一行
<property name="dynamic.classpath" value="true" />
一起学编程,让生活更随和!如果你觉得是个同道中人,欢迎关注博主gzh:【随和的皮蛋桑】。专注于Java基础、进阶、面试以及计算机基础知识分享。偶尔认知思考、日常水文。