java - 如何 Autowiring RedisTemplate<String,Long>

标签 java spring spring-mvc redis

我想在 spring boot 上使用 RedisTemplate。 我可以成功使用 StringRedeisTemplate ,但是当我不能使用 RedisTemplate 时。 这是代码。

@Service
public class MyService {

    @Autowired
    private RedisTemplate<String, Long> template;

    public void execute() {
        template.opsForValue().set("hoge", 1l);
    }
}

但是,当启动应用程序时,会出现错误。

> Exception in thread "main"
> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'MyService': Injection of autowired
> dependencies failed; nested exception is
> org.springframework.beans.factory.BeanCreationException: Could not
> autowire field: private
> org.springframework.data.redis.core.RedisTemplate
> org.hoge.service.MyService.template; nested exception is
> org.springframework.beans.factory.NoSuchBeanDefinitionException: 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.java:334)
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
>   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:302)
>   at
> org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
>   at
> org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
>   at
> org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
>   at
> org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762)
>   at
> org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
>   at
> org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
>   at
> org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
>   at
> org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
>   at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
>   at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:961)
>   at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:950)
>   at
> jp.bizreach.davide.recommend.application.DavimendApplication.main(DavimendApplication.java:11)
>   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>   at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>   at java.lang.reflect.Method.invoke(Method.java:483)     at
> com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
> Caused by: org.springframework.beans.factory.BeanCreationException:
> Could not autowire field: private
> org.springframework.data.redis.core.RedisTemplate
> org.hoge.service.MyService.template; nested exception is
> org.springframework.beans.factory.NoSuchBeanDefinitionException: 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$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:558)
>   at
> org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
>   at
> org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
>   ... 21 more Caused by:
> org.springframework.beans.factory.NoSuchBeanDefinitionException: 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308)
>   at
> org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
>   at
> org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
>   at
> org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:530)
>   ... 23 more

最佳答案

堆栈跟踪表明您还没有定义要用于在 RedisTemplate 中注入(inject)的 Bean。您可以通过创建配置文件来解决它,例如

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class AppConfig {
 @Bean
 JedisConnectionFactory jedisConnectionFactory() {
  return new JedisConnectionFactory();
 }

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

一旦有了配置文件,就需要将其传递给 SpringApplication.run 例如

Object[] sources = {AppConfig.class};
ApplicationContext ctx = SpringApplication.run(sources, args);

关于java - 如何 Autowiring RedisTemplate<String,Long>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27521672/

相关文章:

java - 在 Spring Security 中登录后点击登录网址

java - 有更有效的方法来处理这个问题吗?

java - 如何使用 ArrayList<myClass> 执行相当于 "myClass[].class"的操作?

java - QuerydslBinderCustomizer 在 Spring Data JPA 2.0.7 中不起作用

java - Spring Boot - 在应用程序启动期间/之前运行代码的正确方法?

java - 是否有Java/Spring Web应用程序的表单生成器?

java - java中set如何存储唯一数据

java - Glassfish 3.01 上的 JSP UTF-8 字符编码错误

java - com.mysql.jdbc.MysqlDataTruncation : Data truncation: Data too long for column 'DATE' at row 1

java - 在 Spring 中通过 JNDI 控制日志文件位置?