Java – How to autowire RedisTemplate

javaredisspring-bootspring-dataspring-data-redis

I want to use RedisTemplate in spring boot. I can use StringRedisTemplate successfully, but I cant use RedisTemplate. here is the code.

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisEntityTests {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate<String, RedisEntity> redisTemplate;

    // This test case can run successfully.
    @Test
    public void testString() {
        // save string
        stringRedisTemplate.opsForValue().set("aaa", "111");
        Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa"));
    }

    // This test case I got error.
    @Test
    public void testObject() throws Exception {
        // save object
        RedisEntity redisEntity = new RedisEntity("Tom", 20);
        redisTemplate.opsForValue().set(redisEntity.getName(), redisEntity);

        Assert.assertEquals(20, (redisTemplate.opsForValue().get("Tom")).getAge().longValue());
    }
}

Then, run the test method:testObject(), Here is the error report:

2017-12-17 16:12:12.079 ERROR 4708 — [ main]
o.s.test.context.TestContextManager : Caught exception while
allowing TestExecutionListener
[org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@49070868]
to prepare test instance [springworld.RedisEntityTests@2f3cd727]

org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'springworld.RedisEntityTests':
Unsatisfied dependency expressed through field 'redisTemplate'; nested
exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
'org.springframework.data.redis.core.RedisTemplate' available: expected at least 1 bean
which qualifies as autowire candidate. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:386)
~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
~[spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
~[spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44)
~[spring-boot-test-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE] at
org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
~[spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) [spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
[spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
[junit-4.12.jar:4.12] at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
[spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
[spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
[spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
[junit-4.12.jar:4.12] at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
[junit-4.12.jar:4.12] at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
[junit-4.12.jar:4.12] at
org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
[junit-4.12.jar:4.12] at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
[junit-4.12.jar:4.12] at
org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
[spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
[spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.junit.runners.ParentRunner.run(ParentRunner.java:363)
[junit-4.12.jar:4.12] at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
[spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
[.cp/:na] at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
[.cp/:na] at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)
[.cp/:na] at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
[.cp/:na] at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
[.cp/:na] at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)
[.cp/:na] Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
'org.springframework.data.redis.core.RedisTemplate' available: expected at least 1 bean
which qualifies as autowire candidate. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] … 29 common
frames omitted

Best Solution

You haven't defined the Bean which you will like to use for injecting in RedisTemplate.You can resolve it creating a configuration file.

@Bean
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }

    @Bean
    RedisTemplate< String, Object > redisTemplate() {
        final RedisTemplate< String, Object > template =  new RedisTemplate< String, Object >();
        template.setConnectionFactory( jedisConnectionFactory() );
        template.setKeySerializer( new StringRedisSerializer() );
        template.setHashValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
        template.setValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
        return template;
    }
Related Question