복붙노트

[REDIS] 어떻게 autowire가 RedisTemplate <문자열, 롱>

REDIS

어떻게 autowire가 RedisTemplate <문자열, 롱>

나는 봄 부팅에 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

해결법

  1. ==============================

    1.스택 트레이스는 당신이 예를 들어, 구성 파일을 작성 해결할 수 RedisTemplate.You에 주입을 위해 사용하고자 할 빈을 정의하지 않은 제안

    스택 트레이스는 당신이 예를 들어, 구성 파일을 작성 해결할 수 RedisTemplate.You에 주입을 위해 사용하고자 할 빈을 정의하지 않은 제안

    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);
    
  2. ==============================

    2.당신이 봄 부팅을 사용하는 경우, redisTemplate 콩에 나타납니다 스프링 부팅 스타터 - 레디 스에 종속성을 추가합니다. 그러나, 콩 유형 RedisTemplate <개체, 개체>입니다,

    당신이 봄 부팅을 사용하는 경우, redisTemplate 콩에 나타납니다 스프링 부팅 스타터 - 레디 스에 종속성을 추가합니다. 그러나, 콩 유형 RedisTemplate <개체, 개체>입니다,

    그래서 다음과 같이 여기 링크 설명을 입력 필요로하는 작업에 대한 또 다른 빈을 만들

  3. ==============================

    3.sol4me 상단에 말했듯이 당신은 할 수 있지만 당신은 쉬운 방법으로이 문제를 처리 할 수 ​​있습니다 :

    sol4me 상단에 말했듯이 당신은 할 수 있지만 당신은 쉬운 방법으로이 문제를 처리 할 수 ​​있습니다 :

    1, 자동 와이어 RedisTemplate

    @Autowired
    private RedisTemplate redisTemplate;
    

    이와 같은 제 2 세트의 값 :

    redisTemplate.opsForValue().set("yourkey", 12434L);
    

    3,이 같은 값을 얻을 :

    (Long) redisTemplate.opsForValue().get("yourkey");
    

    당신이 그런 식으로이 작업을 수행 할 경우 RedisTemplate <문자열, 롱>와 같은 빈을 만들 필요가, 당신은 당신의 시스템에 콩을 많이 만들 수 없습니다. 그러니 그냥 쉬운 방법을 사용합니다.

  4. ==============================

    4.지금은 2018대로, 뭔가 다른주세요 (뭔가 많이 변경하는 시간이 오래 블록 나를)

    지금은 2018대로, 뭔가 다른주세요 (뭔가 많이 변경하는 시간이 오래 블록 나를)

  5. from https://stackoverflow.com/questions/27521672/how-autowired-redistemplatestring-long by cc-by-sa and MIT license