我正在开发一个 Spring-MVC 应用程序,我想在其中将 Redis 用于 Redis 键值对中的字符串和整数值。我的意图是在传递字符串时检索整数。当我试图检查我正在尝试的配置是否正确时,我收到了一个错误。
我有 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 永远找不到,我尝试了很多路径。如果有更好的选择,我想知道。非常感谢。如果需要更多信息,请告诉我。非常感谢。