Java 带有自定义实体的SpringBoot注入模板

Java 带有自定义实体的SpringBoot注入模板,java,spring,redis,spring-boot,Java,Spring,Redis,Spring Boot,在我的SpringBootproejct中,当我使用as来注入RedisTemplate时,一切正常 @Repository public class CommonDBDaoImpl implements CommonDBDao { @Autowired RedisTemplate<String, Object> redisTemplate; .... } 日志: 如何在RedisTemplate中使用自定义实体/DTO?RedisTemplate Red

在我的
SpringBoot
proejct中,当我使用as来注入
RedisTemplate
时,一切正常

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

    ....
}
日志:

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

RedisTemplate RedisTemplate
RedisTemplate RedisTemplate
是两个不同的签名,如果spring找不到第一个bean,则必须手动定义一个新bean

您可以这样做:

    @Bean
    public RedisTemplate<String, PersonDTO> redisTemplatePersonDTO() {
        return new RedisTemplate<String, PersonDTO>() { 
            /* the declaration of the object here */ 
        };
    }
@Bean
公共RedisTemplate redisTemplatePersonDTO(){
返回新的RedisTemplate(){
/*此处的对象声明*/
};
}
将此代码放在ApplicationConfig类中,并确保使用
@EnableAutoConfiguration
@Configuration

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