无法在 Redis 模板中加载连接工厂并且无法找到 spring 配置 xml

Not able to load connection factory in Redis template and unable to find spring config xml

我正在开发一个 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)

组件class。请注意,我在服务文件夹中有这个 class:

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

}

根目录下的Redis配置-context.xml :

 <!-- 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,我尝试了很多路径。如果有更好的选择,我想知道。非常感谢。如果需要更多信息,请告诉我。非常感谢。

这一行是错误的:-

 <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");

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

spring.redis.host=localhost
spring.redis.password=secret
spring.redis.port=6379