Cannot get connection for redisTemplate for Spring redis data

I am trying to post to a feed using Spring Redis data using Jedis. Here is a very simple Java configuration:

@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; } 

where redisPort = 6379 and redisHostName = "localhost".

When I run the following test:

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

I get the following stack:

 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 
+6
source share
9 answers

This issue is caused by a version of Jedis (2.7.2) that is incompatible with Spring Data Redis (1.5.0.RELEASE). I used 3 days, faced with the same question, before I will be inspired by this post and comments. The Jedis version (2.6.2) is working fine (although I hit other errors in my program, but at least it has some progress than the same error message)!

Thanks.

+11
source

Turns out I used Jedis 2.7.2, but Spring Data Redis 1.5.0 seems to be compatible with Jedis 2.6.2. I wish this was a little clearer in the documentation, somehow.

+5
source

Compatible Version:

 <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> 
+2
source

Version, I ran into the same problem, this 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> 
+1
source

Naveen Vishwanath, thanks for the decision! How did you find it using documents or debugs?

For those using Gradle, here is my combination:

Versions

  • Redis 3.0.2
  • Jedis 2.7.2
  • Spring Data Redis 1.6.0 M1 (not available in the maven central center because this is a release release - add 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' } 

Please note that Spring Data Redis 1.6.0.M1 will be removed after its release, you need to change it to 1.6.0.RELEASE when it will be available as release.

So, I checked the compatibility version here: https://github.com/spring-projects/spring-data-redis/blob/master/gradle.properties and found the corresponding JIRA ticket here: <a2>

+1
source

I also ran into a similar problem. I did some research and found out that this is due to a conflict in the bank.

The compatible version that I use in my application:

 <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> 

Or, if you are using spring boot , just add the following dependency. Spring boot is smart enough to automatically resolve such issues.

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

Hope this helps!

+1
source

there was a similar problem with spring-boot-starter-data-redis 2.0.4

when I implicitly put the dependency for the Jedi in the pom.xml application, it somehow worked

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

Eclipse complains about “Duplicate Managed Version”, but everything else works

+1
source

For those using Spring-boot-starter-data-redis

A permanent fix can be entered as follows:

  • Determine the version of Spring-boot-starter-data-redis. In eclipse, you can hover over writing dependencies in a pom.xml file. He will tell you the version that you are using.
  • Go to the directory of this version of your maven repository.
  • Edit the pom file and add the version number of the redis client that is needed. In this case, version 2.6.2.RELEASE should come with version 1.50 from Spring-boot-starter-data-redis. In this case, I had to add a <version>2.6.2.RELEASE</version .
  • In your IDE, upgrade the entire project. To do this in eclipse or STS, click once on the project name-> right click select maven-> Update Project
  • Check out the maven dependency list and the jedis version should now be 2.6.2.
  • Compile your project.
  • Open the .war archive with jar or 7-zip etc. Locate the lib directory in the WEB-INF section and check for jedis-2.6.2.

This should solve the problem.

0
source

Compatible Version:

 <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> 
0
source

Source: https://habr.com/ru/post/987891/


All Articles