Search code examples
javaspringspring-mvcredisspring-data-redis

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


I am working on a Spring-MVC application in which I would like to use Redis to a String and an Integer value in the Redis Key-Value pair. My intention is to retrieve the Integer whenever I pass the String. I am getting an error when I am just trying to check if the configuration I am trying is correct.

I am having 2 problems, I am getting an error when I am trying to just run the project to see if my configuration is correct(error log posted below). Secondly, I don't know how to get the instance of UserAppRegistration instance from spring, other than to pass the XML file and get the context. This approach has not worked for me.

Error log :

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)

The component class. Please note I have this class in Service folder:

@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 configuration in root-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"/>

Now, I would like to retrieve it the UserAppRegistration component to push and pull values from it. The way, suggested was this :

public static UserAppRegistration userAppRegistration;

public static ClassPathXmlApplicationContext context;

static {
   context = new ClassPathXmlApplicationContext("root-context.xml");
      }

For some reason, this way has never worked for me before. The xml is never found, I have tried many paths. If there is any better alternative, I would like to know. Thanks a lot. If there is any more information required, kindly let me know. Thanks a lot.


Solution

  • This line is wrong:-

     <beans:bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connectionFactory="jedisConnFactory"/>
    

    Instead of above it should be like:-

     <beans:bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnFactory"/>
    

    You can retrieve the UserAppRegistration bean like below:-

    UserAppRegistration bean = (UserAppRegistration)context.getBean("userAppRegistration");
    

    To load xml from complete path:-

    context = new FileSystemXmlApplicationContext("C:/Users/project/root-context.xml");