【问题标题】:Redis Session with Java Spring boot带有 Java Spring 启动的 Redis 会话
【发布时间】:2019-03-15 12:49:18
【问题描述】:

我正在尝试保存我的会话实例,而 Redis 服务器在本地主机上损坏。当我启动应用程序时,我收到错误消息并且应用程序无法启动。 看起来找不到类或 bean 错误,但我不太了解错误消息。

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.739 s <<< FAILURE! - in com.kb.webkb.WebkbApplicationTests
[ERROR] contextLoads(com.kb.webkb.WebkbApplicationTests)  Time elapsed: 0.005 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisMessageListenerContainer' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]: Unsatisfied dependency expressed through method 'redisMessageListenerContainer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'connectionFactory' defined in class path resource [com/kb/webkb/configuration/session/SessionConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.jedis.JedisConnectionFactory]: Factory method 'connectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/util/Pool
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'connectionFactory' defined in class path resource [com/kb/webkb/configuration/session/SessionConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.jedis.JedisConnectionFactory]: Factory method 'connectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/util/Pool
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.jedis.JedisConnectionFactory]: Factory method 'connectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/util/Pool
Caused by: java.lang.NoClassDefFoundError: redis/clients/util/Pool
Caused by: java.lang.ClassNotFoundException: redis.clients.util.Pool

它在 SessionConfiguration 类中使用 Jedis 来调用 Jedis 对象。

@Configuration
@EnableRedisHttpSession
public class SessionConfig extends AbstractHttpSessionApplicationInitializer {
    @Bean
    public JedisConnectionFactory connectionFactory() {
        return new JedisConnectionFactory();
    }
}

我已经尝试了这篇文章的解决方案:stackoverflow.com/q/33128318/11189140

但是当我这样做时,我/正在收到此错误

Description:

An attempt was made to call the method org.springframework.data.redis.connection.RedisConnection.getConfig(Ljava/lang/String;)Ljava/util/List; but it does not exist. Its class, org.springframework.data.redis.connection.RedisConnection, is available from the following locations:

    jar:file:/home/kbuczynski/.m2/repository/org/springframework/data/spring-data-redis/2.1.5.RELEASE/spring-data-redis-2.1.5.RELEASE.jar!/org/springframework/data/redis/connection/RedisConnection.class

It was loaded from the following location:

    file:/home/kbuczynski/.m2/repository/org/springframework/data/spring-data-redis/2.1.5.RELEASE/spring-data-redis-2.1.5.RELEASE.jar

POM.XML

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.kb</groupId>
    <artifactId>webkb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>webkb</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
            <version>1.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

  • 这里似乎已经回答了:stackoverflow.com/q/33128318/11189140
  • @AnirudhSimha 我确实找到了这个解决方案,并且我确实尝试过,但我得到了一个不同的错误。我编辑了我的帖子,所以你可以看到失败的原因。

标签: java spring spring-boot spring-session spring-data-redis


【解决方案1】:

在您的错误消息中,自 Jedis 3 以来,NoClassDefFoundError: redis/clients/util/Pool 位于 redis/clients/jedis/util/Pool 中。

请检查您的(托管)依赖项 (mvn dependency:tree),如果您仍然在 2.x 版本中使用 Jedis。

至少Spring Data Redis 2.1.x 正在使用Jedis 2.9.3,其中redis/clients/util/Pool 仍然有效。因此,我怀疑您的情况存在相互冲突的依赖关系。

附录

感谢您也分享您的project。现在很清楚发生了什么:关键部分是你的

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session</artifactId>
    <version>1.2.2.RELEASE</version>
</dependency>

请注意,这是 Spring Boot 的 托管依赖,但是您强制它使用版本 1.2.2.RELEASE。从你的依赖中删除这个,spring-session-data-redis 正在为你做你需要的一切。

我已经在我的分叉存储库中提交了这个以及一个集成测试,并为你创建了一个pull request

请验证它是否也适合您!

【讨论】:

  • 看起来我正在使用 [INFO] +- org.springframework.session:spring-session-data-redis:jar:2.1.4.RELEASE:compile [INFO] | +- org.springframework.data:spring-data-redis:jar:2.1.5.RELEASE:compile [INFO] \- org.springframework.boot:spring-boot-starter-data-redis:jar:2.1.3。 RELEASE:compile Idea 如果出现问题如何解决?我真的不明白如何解决它。
  • 您的确切配置和 pom.xml 中的依赖项在这里为我工作。当我问mvn dependency:tree 时,它会显示以下内容:[INFO] +- redis.clients:jedis:jar:2.9.1:compile – 你能验证一下吗?
  • 您好,我创建了新项目,重用了我提供的 pom.xml 文件。我设法摆脱了烦人的错误,但是我被困住了尝试调用方法 org.springframework.data.redis.connection.RedisConnection.getConfig(Ljava/lang/String;)Ljava/util/List;但它不存在。它的类 org.springframework.data.redis.connection.RedisConnection 可从以下位置获得: 它建议修复类路径,使其包含单个兼容版本的 spring reddis。有什么建议吗?
  • 你能分享你的新项目吗,也许在 Github 上?
  • 这个答案对我帮助很大!我以下参考也有帮助:docs.spring.io/spring-boot/docs/current/reference/htmlsingle/…
猜你喜欢
  • 2015-05-07
  • 1970-01-01
  • 1970-01-01
  • 2022-10-13
  • 2016-04-08
  • 2016-12-05
  • 1970-01-01
  • 1970-01-01
  • 2016-12-31
相关资源
最近更新 更多