0

我正在开发一个 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 永远找不到,我尝试了很多路径。如果有更好的选择,我想知道。非常感谢。如果需要更多信息,请告诉我。非常感谢。

4

2 回答 2

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");
于 2015-08-19T07:54:09.463 回答
1

您也可以在application.properties中配置它:

spring.redis.host=localhost
spring.redis.password=secret
spring.redis.port=6379
于 2015-11-18T20:07:26.210 回答