【问题标题】:Cannot get connection for redisTemplate for Spring data redis无法为 Spring 数据 redis 获取 redisTemplate 的连接
【发布时间】:2015-08-07 11:59:55
【问题描述】:

我正在尝试使用 Jedis 使用 Spring 数据 Redis 将消息发布到通道。这是一个非常简单的 Java 配置:

@Bean(name="jedisConnectionFactory")
JedisConnectionFactory jedisConnectionFactory() {
    JedisConnectionFactory factory = new JedisConnectionFactory();
    factory.setHostName(redisHostName);
    factory.setPort(redisPort);
    factory.setUsePool(true);
    return factory;
}

@Bean(name="redisTemplate")
RedisTemplate<Object, Object> redisTemplate() {
    RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
    redisTemplate.setConnectionFactory(jedisConnectionFactory());
    return redisTemplate;
}

其中 redisPort=6379 和 redisHostName="localhost"。

当我运行以下测试时:

 @Test
public void testRedis(){
    ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
    RedisTemplate<Object,Object> redisTemplate = (RedisTemplate<Object, Object>) context.getBean("redisTemplate");
    redisTemplate.convertAndSend("test", "123");
}

我得到以下堆栈跟踪:

java.lang.ExceptionInInitializerError
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:252)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:58)
at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:128)
at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:91)
at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:78)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:178)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:153)
at org.springframework.data.redis.core.RedisTemplate.convertAndSend(RedisTemplate.java:676)
at com.jobvite.realtimeanalytics.redis.RedisTest.testRedis(RedisTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75) 
Caused by: java.lang.NullPointerException
at org.springframework.util.ReflectionUtils.makeAccessible(ReflectionUtils.java:443)
at org.springframework.data.redis.connection.jedis.JedisConnection.<clinit>(JedisConnection.java:108)
... 44 more

【问题讨论】:

    标签: spring-data spring-data-redis


    【解决方案1】:

    原来我使用的是 Jedis 2.7.2,但 Spring Data Redis 1.5.0 似乎与 Jedis 2.6.2 兼容。我希望这在文档中能更清楚一点。

    【讨论】:

    • 同样适用于 spring data redis 1.4.4-RELEASE。有一个托管依赖关系(我找不到从哪里... grrrrr)获取 Jedis 2.5.2
    • 我知道这是一个旧线程,但我遇到了这个问题并且有解决方案。
    【解决方案2】:

    版本,我也遇到了同样的问题,这个spring-data-redis 1.5.0 pom

    <?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>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.5.0.RELEASE</version>
      <name>Spring Data Redis</name>
      <description>Spring Data Redis</description>
      <url>http://github.com/spring-projects/spring-data-redis</url>
      <organization>
        <name>Pivotal Software, Inc.</name>
        <url>http://projects.spring.io/spring-data-redis</url>
      </organization>
      <licenses>
        <license>
          <name>The Apache Software License, Version 2.0</name>
          <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
          <distribution>repo</distribution>
        </license>
      </licenses>
      <developers>
        <developer>
          <id>costin</id>
          <name>Costin Leau</name>
          <email>cleau@vmware.com</email>
          <properties>
            <twitter>costinl</twitter>
          </properties>
        </developer>
        <developer>
          <id>jencompgeek</id>
          <name>Jennifer Hickey</name>
          <properties>
            <twitter>jencompgeek</twitter>
          </properties>
        </developer>
        <developer>
          <id>christophstrobl</id>
          <name>Christoph Strobl</name>
          <properties>
            <twitter>stroblchristoph</twitter>
          </properties>
        </developer>
        <developer>
          <id>thomasdarimont</id>
          <name>Thomas Darimont</name>
          <properties>
            <twitter>thomasdarimont</twitter>
          </properties>
        </developer>
      </developers>
      <scm>
        <connection>scm:git:git://github.com/spring-projects/spring-data-redis</connection>
        <developerConnection>scm:git:git://github.com/spring-projects/spring-data-redis</developerConnection>
        <url>http://github.com/spring-projects/spring-data-redis</url>
      </scm>
      <dependencies>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-oxm</artifactId>
          <version>4.0.9.RELEASE</version>
          <scope>compile</scope>
          <optional>true</optional>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>4.0.9.RELEASE</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.codehaus.jackson</groupId>
          <artifactId>jackson-mapper-asl</artifactId>
          <version>1.8.8</version>
          <scope>compile</scope>
          <optional>true</optional>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
          <version>4.0.9.RELEASE</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>4.0.9.RELEASE</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.jredis</groupId>
          <artifactId>jredis-core-ri</artifactId>
          <version>06052013</version>
          <scope>compile</scope>
          <optional>true</optional>
        </dependency>
        <dependency>
          <groupId>redis.clients</groupId>
          <artifactId>jedis</artifactId>
          <version>2.6.2</version>
          <scope>compile</scope>
          <optional>true</optional>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>4.0.9.RELEASE</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>4.0.9.RELEASE</version>
          <scope>compile</scope>
          <exclusions>
            <exclusion>
              <artifactId>commons-logging</artifactId>
              <groupId>commons-logging</groupId>
            </exclusion>
          </exclusions>
        </dependency>
        <dependency>
          <groupId>commons-beanutils</groupId>
          <artifactId>commons-beanutils-core</artifactId>
          <version>1.8.3</version>
          <scope>compile</scope>
          <optional>true</optional>
        </dependency>
        <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-pool2</artifactId>
          <version>2.2</version>
          <scope>compile</scope>
          <optional>true</optional>
        </dependency>
        <dependency>
          <groupId>com.github.spullara.redis</groupId>
          <artifactId>client</artifactId>
          <version>0.7</version>
          <scope>compile</scope>
          <optional>true</optional>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.5.1</version>
          <scope>compile</scope>
          <optional>true</optional>
        </dependency>
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.7.10</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>com.lambdaworks</groupId>
          <artifactId>lettuce</artifactId>
          <version>2.3.3</version>
          <scope>compile</scope>
          <optional>true</optional>
        </dependency>
        <dependency>
          <groupId>org.jredis</groupId>
          <artifactId>jredis-core-api</artifactId>
          <version>06052013</version>
          <scope>compile</scope>
          <optional>true</optional>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-core</artifactId>
          <version>2.5.1</version>
          <scope>compile</scope>
          <optional>true</optional>
        </dependency>
      </dependencies>
    </project>
    

    【讨论】:

      【解决方案3】:

      兼容版本:

      <dependency>
          <groupId>redis.clients</groupId>
          <artifactId>jedis</artifactId>
          <version>2.7.0</version>
      </dependency>
      <dependency>
          <groupId>org.springframework.data</groupId>
          <artifactId>spring-data-redis</artifactId>
          <version>1.5.0.RELEASE</version>
      </dependency>
      

      【讨论】:

        【解决方案4】:

        此问题是由于 Jedis 版本 (2.7.2) 与 Spring Data Redis (1.5.0.RELEASE) 不兼容造成的。在我受到这篇文章和评论的启发之前,我用了 3 天的时间来面对同样的问题。 Jedis 版本(2.6.2)工作正常(虽然我在程序中遇到了其他错误,但至少它比相同的错误消息有一些进步)!

        谢谢。

        【讨论】:

        • 很高兴它有帮助。我想我们需要注意 POM 以确保版本兼容,因为当您遇到这些问题时,堆栈跟踪通常没有帮助。
        • 你拯救了我的一天@kwky。顺便说一句,我真的很困惑,春天以一种非常糟糕的方式抑制异常。他们确实需要改进 JedisConnectionFactory 的代码。
        • 是的。这就是为什么我花了这么长时间来解决我的问题。顺便说一句,我们总是可以注意 maven 中列出的某些 jar 的编译依赖项。我认为建议使用与此处列出的相同版本,以避免再次发生类似问题。
        【解决方案5】:

        Navin Viswanath,感谢您的解决方案!您是如何找到它的,使用文档还是调试?

        对于那些使用 Gradle 的人,这是我的组合:

        版本

        • Redis 3.0.2
        • 绝地武士 2.7.2
        • Spring Data Redis 1.6.0 M1(在 maven Central 中不可用,因为它是里程碑版本 - 添加 http://repo.spring.io/milestone/ repo)

        build.gradle

        repositories {
          mavenCentral()
          maven { url 'http://repo.spring.io/release/' }
          maven { url 'http://repo.spring.io/milestone/' }
        }
        
        dependencies {
          compile group: 'redis.clients', name: 'jedis', version: '2.7.2'
          compile group: 'org.springframework.data', name: 'spring-data-redis', version: '1.6.0.M1'
        }
        

        请注意,Spring Data Redis 1.6.0.M1 在发布后将被删除,您需要在发布时将其更改为 1.6.0.RELEASE

        所以,我在这里检查了版本兼容性:https://github.com/spring-projects/spring-data-redis/blob/master/gradle.properties 并在这里找到了相关的 JIRA 票证:https://jira.spring.io/browse/DATAREDIS-396

        【讨论】:

          【解决方案6】:

          对于那些使用 Spring-boot-starter-data-redis 的人

          可以按如下方式输入永久修复:

          1. 确定 spring-boot-starter-data-redis 的版​​本。在 eclipse,您可以将鼠标悬停在 pom.xml 中的依赖项上 文件。它会告诉您正在使用的版本。
          2. 导航到您的 Maven 存储库中的该版本目录。
          3. 编辑pom文件,添加需要的redis客户端版本号。在这种情况下,版本 2.6.2.RELEASE 与版本一起使用 spring-boot-starter-data-redis 的 1.50。在这种情况下,我必须添加 &lt;version&gt;2.6.2.RELEASE&lt;/version
          4. 在您的 IDE 中,刷新整个项目。要在 eclipse 或 STS 中执行此操作,请单击一次项目名称->右键单击选择 maven->更新项目
          5. 检查您的 maven 依赖项列表,jedis 版本现在应该是 2.6.2。
          6. 编译您的项目。
          7. 用jar或7-zip等打开.war压缩包,找到WEB-INF下的lib目录,验证jedis-2.6.2是否存在。

          这应该可以解决问题。

          【讨论】:

            【解决方案7】:

            我也遇到过类似的问题。我做了一些研究,发现这是由于 jar 冲突。

            兼容版本我在我的应用程序中使用的是:

            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
                <version>1.8.10.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.0</version>
            </dependency>
            

            或者如果您使用的是 spring boot,只需添加以下依赖项。 Spring Boot 足够智能,可以自动解决此类问题。

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            

            希望对你有帮助!!

            【讨论】:

            • 原来 spring boot 并不总是足够聪明。 spring-boot-starter-data-redis 依赖项有同样的问题。已通过使用单独的 spring-data-redis 和 jedis 依赖项解决。
            【解决方案8】:

            spring-boot-starter-data-redis 2.0.4 也有类似问题

            当我在应用程序 pom.xml 中隐含地把对 jedis 的依赖项放在它以某种方式工作时

                <dependency>
                    <groupId>redis.clients</groupId>
                    <artifactId>jedis</artifactId>
                    <version>2.9.0</version>
                </dependency>
            

            eclipse 抱怨“复制托管版本”,但其他一切正常

            【讨论】:

              【解决方案9】:

              兼容版本:

              <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-data-redis</artifactId>
                      <version>2.0.4.RELEASE</version>
                  </dependency>
                  <dependency>
                      <groupId>redis.clients</groupId>
                      <artifactId>jedis</artifactId>
                      <version>2.9.0</version>
              </dependency>
              

              【讨论】:

              • 对。这是兼容性问题。
              【解决方案10】:

              您可以从以下链接中找到与 jedis maven 版本兼容的 spring data maven 版本:https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis/1.5.9.RELEASE

              【讨论】:

                【解决方案11】:

                自 2019 年 7 月起,Spring Boot 2.X 与 Jedis 3.X 兼容(请参阅 Upgrade to Jedis 3.1.0 了解更多信息)。 对于使用 Gradle 的人来说,一个例子如下:

                build.gradle

                plugins {
                    id 'org.springframework.boot' version '2.3.2.RELEASE'
                    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
                    id 'java'
                }
                
                group = 'com.example'
                version = '0.0.1-SNAPSHOT'
                sourceCompatibility = '1.8'
                
                repositories {
                    mavenCentral()
                }
                
                dependencies {
                    implementation 'org.springframework.boot:spring-boot-starter-data-redis'
                    implementation 'redis.clients:jedis:3.1.0'
                }
                

                【讨论】:

                  猜你喜欢
                  • 2017-06-17
                  • 2016-02-28
                  • 2019-04-22
                  • 2020-04-19
                  • 1970-01-01
                  • 2020-07-05
                  • 1970-01-01
                  • 2015-10-15
                  • 1970-01-01
                  相关资源
                  最近更新 更多