java - 带有自定义实体的SpringBoot注入(inject)RedisTemplate

标签 java spring redis spring-boot

在我的SpringBoot项目中,当我使用如下注入(inject)RedisTemplate时,没问题。

@Repository
public class CommonDBDaoImpl implements CommonDBDao {
    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    ....
}

但是,当我将 RedisTemplate 与自定义 Entity/DTO 一起使用时,注入(inject)失败..

@Repository
public class CommonDBDaoImpl implements CommonDBDao {
    @Autowired
    RedisTemplate<String, PersonDTO> redisTemplate;

    ....
}

public PersonDTO implements Serializable {
    //field
    //getter and setter
}

日志:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'commonDBDaoImpl': Injection of autowired dependenci
es failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.data.red
is.core.RedisTemplate com.java.my.dao.CommonDBDaoImpl.redisTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDe
finitionException: No qualifying bean of type [org.springframework.data.redis.core.RedisTemplate] found for dependency: expected at least 1 bean which
 qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.j
ava:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:101)
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:68)
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86)
    ... 25 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.data.redis.core.RedisTemplat
e com.java.my.dao.CommonDBDaoImpl.redisTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: N
o qualifying bean of type [org.springframework.data.redis.core.RedisTemplate] found for dependency: expected at least 1 bean which qualifies as autowi
re candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcesso
r.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.j
ava:331)
    ... 40 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.redis.core.RedisTempl
ate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springfra
mework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcesso
r.java:533)
    ... 42 more

如何在 RedisTemplate 中使用自定义 Entity/DTO

最佳答案

RedisTemplate<String, PersonDTO> redisTemplateRedisTemplate<String, Object> redisTemplate有两个不同的签名那么 spring 找不到第一个的 bean,你必须手动定义一个新的 bean。

你可以这样做:

    @Bean
    public RedisTemplate<String, PersonDTO> redisTemplatePersonDTO() {
        return new RedisTemplate<String, PersonDTO>() { 
            /* the declaration of the object here */ 
        };
    }

将此代码放在您的 ApplicationConfig 类中,并确保用 @EnableAutoConfiguration 对其进行注释和 @Configuration

关于java - 带有自定义实体的SpringBoot注入(inject)RedisTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29891769/

相关文章:

java - 我可以将 SpringEL 的 boolean 值或运算符与 Spring 安全的 @Preauthorize 一起使用吗?

c# - 使用 BookSleeve 维护一个开放的 Redis 连接

redis - REDIS INFO中active_defrag_running的值是什么意思?

java - 单击按钮时旋转图像而不是单击按钮时停止图像

java - 如何在Spring MVC mysql DAO连接中使用try catch?

java - wcf 服务在 java 项目中运行良好,但在 android 项目中失败

node.js - JWT 撤销信息存放在哪里,mongoDB 还是 Redis?

java - 在将 Sockets 与 NIO 结合使用时防止内存使用率过高

java - 如何使用 spring 和 hibernate 从 mysql 获取图像列表

java - Spring 自定义格式化程序不工作