1.问题
近期遇到的一个异常问题如题所示,下面是完整的异常内容,原景重现:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'redisFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/jedis/util/Pool
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Disconnected from the target VM, address: '127.0.0.1:60159', transport: 'socket'
Exception in thread "main" java.lang.ExceptionInInitializerError
at xxx.xxx.xxx.lifecycle_calc.source.KafkaSource.<init>(KafkaSource.java:14)
at xxx.xxx.xxx.lifecycle_calc.LifecycleCalcStartup.main(LifecycleCalcStartup.java:31)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisHelper' defined in URL [file:/D:/Java/Workplace/fargo-crm-backend/fargo-lifecycle-calc/target/classes/config/spring-redis.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'redisFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/jedis/util/Pool
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1736)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1444)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at xxx.xxx.xxx.lifecycle_calc.factory.ApplicationContextFactory.<clinit>(ApplicationContextFactory.java:9)
... 2 more
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'redisFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/jedis/util/Pool
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:122)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:77)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1732)
... 15 more
Process finished with exit code 1
异常提示:在方法调用redisFactory时,因找不到路径redis/clients/jedis/util/Pool的这个类,而导致的报错!
2.原因
先说一下我的本地项目环境情况:spring-boot版本2.2.5.RELEASE,jedis使用的版本是2.9.3
2.1 分析一
既然提示找不到这个包,那么先看看我们的项目中,依赖包中是否能从指定路径找到这个类???
从路径结构,发现pool确实不是在jedis下面,而是在与jedis同级的util目录下,所以才报错找不到该类!!!
2.2 换依赖包
通过了解,得知jedis1.x版本与2.x版本有所差别,与springboot的版本有相对应的依赖关系!
spring-data-redis版本 | jedis版本 |
2.1.x | 2.9.x |
2.2.x | 3.x |
于是把jedis版本换成了3.1.0版本:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>
再看指定的目录是否有对应的类?果然,下面这个目录结构是可以找到的:
重新启动,本以为胜利的曙光就在眼前了!怎料,控制台出现如下显示:
但是程序并没有退出提示,说明此时程序已经启动了,只是日志没有正常打印出来而已!
3.解决
通过上面的分析处理,其实在上述V2.2换依赖包时,我们主要的问题已经解决啦(也就是如题的问题)!!!但是,控制台却没有日志打印,这多少让人觉得好像还是没能正常启动~~~
所以,我们需要再看看日志为啥子没有打印出来!因为主要问题已经找出来了,所以这个日志为啥没打印的问题,我就直接给出我的解决方式吧 !
通过查看包依赖问题,我们发现是依赖里缺少了log日志的包依赖,导致日志没有正常打印!所以加上之后,包依赖情况如下:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
其实上述依赖,改成如下情况,也是ok的:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
因为spring-boot-starter-data-redis中已经包含了相对应的依赖包了,所以只是依赖包的结构不同而已!!!