2014-12-17 12 views
6

Desidero utilizzare RedisTemplate per l'avvio a molla. Posso usare StringRedeisTemplate correttamente, ma quando non posso usare RedisTemplate. ecco il codice.Come avviato automaticamente RedisTemplate <String,Long>

@Service 
public class MyService { 

    @Autowired 
    private RedisTemplate<String, Long> template; 

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

Ma, quando avviare le app, ottenere errori.

> 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 
+0

Hai definito il bean nel tuo xml? Se sì, puoi incollare incollando il codice pertinente. – sol4me

+0

Non uso xml. Uso la configurazione automatica in caso di avvio a molla –

+0

è possibile incollare la configurazione di avvio Spring, in cui è stata definita la redistione? – RE350

risposta

5

Lo stacktrace suggeriscono che non è stata definita la Bean che si desidera utilizzare per l'iniezione in RedisTemplate .È possibile risolverlo creando un file di configurazione, ad esempio

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; 
} 
} 

Una volta ottenuto il file di configurazione allora avete bisogno di passarlo a SpringApplication.run Ad es

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

Grazie mille per la risposta. Ho creato il file di configurazione, quindi posso usare RedisTemplate. –

+0

Grazie, ma perché è necessario aggiungere un bean jedisConnectionFactory indipendente, perché non è possibile solo 'template.setConnectionFactory (new JedisConnectionFactory());' – zhuguowei

+0

@zhuguowei Se non ti piace usare/autowire '' 'JedisConnectionFactory''' in molti luoghi, quindi sentiti libero di inserirlo in linea – sol4me

1

Se si utilizza avvio Primavera, aggiungere una dipendenza in Primavera-boot-starter-Redis, che porterà nel chicco redisTemplate. Tuttavia, che Bean è di tipo RedisTemplate<Object, Object>,

Quindi creare un altro bean per le operazioni di cui avete bisogno, come mostrato qui enter link description here

0

Si può fare sol4me detto sulla parte superiore, ma è possibile gestire questo in un facile modo:

1, legare auto RedisTemplate

@Autowired 
private RedisTemplate redisTemplate; 

2, valore impostato in questo modo:

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

3, ottenere il valore in questo modo:

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

Non c'è bisogno di creare un fagiolo come RedisTemplate< String, Long >, se si esegue questa operazione del genere, si può creare un sacco di fagioli nel vostro sistema. Quindi usa il modo semplice.

+0

utilizzando i tipi Raw non è una buona idea. l'OP voleva usare i tipi generici – asgs

Problemi correlati