我想在spring boot中使用RedisTemplate。我可以成功使用StringRedisTemplate,但我不能使用RedisTemplate。这是代码。
@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());
}
}
然后,运行测试方法:testObject(),这是错误报告:
2017-12-17 16:12:12.079 ERROR 4708 --- [主要] o.s.test.context.TestContextManager:遇到异常时 允许TestExecutionListener [org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@49070868] 准备测试实例[springworld.RedisEntityTests@2f3cd727]
org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为&#39; springworld.RedisEntityTests&#39;的bean时出错: 通过字段“redisTemplate”表达的不满意的依赖性;嵌套 例外是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 合格的bean类型 &#39; org.springframework.data.redis.core.RedisTemplate&#39;可用:预计至少1个豆 有资格成为autowire候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)} 在 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]引起: org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 合格的bean类型 &#39; org.springframework.data.redis.core.RedisTemplate&#39;可用:预计至少1个豆 有资格成为autowire候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)} 在 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常见 框架省略
答案 0 :(得分:1)
您尚未在RedisTemplate中定义要用于注入的Bean。您可以解析它创建配置文件。
@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;
}
答案 1 :(得分:1)
Looks like there is no easy way of doing that. Your problem is similar to DRYing up a generic RedisTemplate in Spring 4
There is some trick with serialization Spring RedisTemplate : Serialise multiple Model classes into JSON.Need to use Multiple RedisTemplates?