我正在开发一个Spring-MVC应用程序,我想在Redis Key-Value对中使用Redis到String和Integer值。我的目的是每当我传递String时检索Integer。当我试图检查我正在尝试的配置是否正确时,我收到错误。
我有2个问题,当我尝试运行项目以查看我的配置是否正确时,我收到错误(错误日志发布在下面)。 其次,除了传递XML文件并获取上下文之外,我不知道如何从spring获取UserAppRegistration实例的实例。这种方法对我没用。
错误日志:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userAppRegistration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.data.redis.core.RedisTemplate com.journaldev.spring.service.UserAppRegistration.redisTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisTemplate' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.data.redis.connection.RedisConnectionFactory' for property 'connectionFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.data.redis.connection.RedisConnectionFactory] for property 'connectionFactory': no matching editors or conversion strategy found
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.data.redis.core.RedisTemplate com.journaldev.spring.service.UserAppRegistration.redisTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisTemplate' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.data.redis.connection.RedisConnectionFactory' for property 'connectionFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.data.redis.connection.RedisConnectionFactory] for property 'connectionFactory': no matching editors or conversion strategy found
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisTemplate' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.data.redis.connection.RedisConnectionFactory' for property 'connectionFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.data.redis.connection.RedisConnectionFactory] for property 'connectionFactory': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
组件类。请注意我在服务文件夹中有这个课程:
@Component
public class UserAppRegistration {
@Autowired
private RedisTemplate<String,Integer> redisTemplate;
public RedisTemplate<String, Integer> getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate<String, Integer> redisTemplate) {
this.redisTemplate = redisTemplate;
}
}
root-context.xml中的Redis配置:
<!-- Configuration for Spring-Data-Redis -->
<beans:bean id="jedisConnFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:usePool="true"/>
<beans:bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connectionFactory="jedisConnFactory"/>
现在,我想检索UserAppRegistration组件以从中推送和提取值。方式,建议如下:
public static UserAppRegistration userAppRegistration;
public static ClassPathXmlApplicationContext context;
static {
context = new ClassPathXmlApplicationContext("root-context.xml");
}
出于某种原因,这种方式以前从未对我有用。从来没有找到xml,我尝试了很多路径。如果有更好的选择,我想知道。非常感谢。如果需要更多信息,请告诉我。非常感谢。
答案 0 :(得分:1)
这一行错了: -
<beans:bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connectionFactory="jedisConnFactory"/>
而不是上面应该是: -
<beans:bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnFactory"/>
您可以检索UserAppRegistration bean,如下所示: -
UserAppRegistration bean = (UserAppRegistration)context.getBean("userAppRegistration");
从完整路径加载xml: -
context = new FileSystemXmlApplicationContext("C:/Users/project/root-context.xml");
答案 1 :(得分:1)
您也可以在 application.properties 中对其进行配置:
spring.redis.host=localhost
spring.redis.password=secret
spring.redis.port=6379