Announcement Announcement Module
Collapse
No announcement yet.
Autowiring RedisTemplate as List/ValueOperations Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowiring RedisTemplate as List/ValueOperations

    I have run into this in the past couple months and just ran into again and have not been able to find an explanation for the problem I keep having when autowiring the RedisTemplate as one of the Operations mechanisms. Essentially, it blows up on server startup and claims that no qualifying bean for ValueOperations or ListOperations can be found. My issue with this is the Spring Redis documentation claims that this is supported. The work around is fine, but it would seem I should not need a work around.

    From the documentation:
    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        
      <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
            p:use-pool="true"/>
      
      <!-- redis template definition -->
      <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
        p:connection-factory-ref="jedisConnectionFactory"/>
    
      ... 
    </beans>
    Code:
    public class Example {
    
      // inject the actual template 
      @Autowired
      private RedisTemplate<String, String> template;
    
      // inject the template as ListOperations
      @Autowired
      private ListOperations<String, String> listOps;
    
      public void addLink(String userId, URL url) {
        listOps.leftPush(userId, url.toExternalForm());
      }
    }
    From my code:
    HTML Code:
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" />
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connectionFactory-ref="jedisConnectionFactory" />
    Code:
    @Autowired 
    @Qualifier("redisTemplate")
    private ListOperations<String, String> listOperations;
    Can anyone explain why this won't work, I've tried it with and without the @qualifier and its the same

    This is the error:
    Code:
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.data.redis.core.ListOperations] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=redisTemplate)}

  • #2
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.j edis.JedisConnectionFactory">
    <property name="hostName" value="host"/>
    <property name="port" value="6379"/>
    <!--<property name="password" value="no password"/>-->
    <property name="timeout" value="2000"/>

    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTe mplate">
    <property name="connectionFactory" ref="jedisConnectionFactory"/>
    <property name="exposeConnection" value="true"/>
    </bean>

    This work perfect for me.

    Comment


    • #3
      whats working?

      When you said that it is working for you is that because it is starting up in the application as a bean in the context, or you are able to autowire it into a managed bean as the interface ValueOperations or ListOperations, one of the interfaces that the documentation says you can wire it in as. With the latest versions I'm still not able to wire the template in as ListOperations, and I'm not seeing any difference in your configuration vs. mine. Its what is written in section 4.4 of the spring redis docs.

      thanks

      Comment


      • #4
        Yeah its working perfectly fine and all operations are work pretty well. I use Value, List, Set operations.

        @Component
        public class NodeCache
        {

        @Autowired
        private RedisTemplate<String, String> template;
        private RedisList<String> nodeCache;
        private RedisAtomicLong nodeIdCounter;
        private ValueOperations<String, String> nodeValueOps;


        @PostConstruct
        public void init()
        {
        nodeValueOps = template.opsForValue();
        nodeCache = new DefaultRedisList<String>(KeyUtils.xxxx(), template);
        nodeIdCounter = new RedisAtomicLong(KeyUtils.xxxx(), template.getConnectionFactory());
        }

        public void addToNodeCache(Node node)
        {
        .......
        }
        }
        I am just working on the it ... but operations are working fine for me.

        Comment


        • #5
          Gabron, what version of spring and spring-data are you using? The issue was reported in the past and it's something related to the way the container does wiring - the latest versions of 3.x and 3.1.x in particular support this case but if you're running an older version, you might troubles.

          Comment


          • #6
            Spring 3.0 with spring-data-redis-1.0.0.RELEASE

            Comment


            • #7
              Can you be more specific about Spring 3.0,x? 3.0.7 is it?

              Comment


              • #8
                Spring 3.0.6.RELEASE

                Comment


                • #9
                  Can you try upgrading to 3.0.7.RELEASE and report back?

                  Thanks,

                  Comment


                  • #10
                    Sorry, I had several things come up in the last week. I'm currently running 3.0.5, I will upgrade it to 3.0.7 and let you know if that fixed it.

                    Comment


                    • #11
                      I upgraded to 3.0.7 and am using spring data redis 1.0.0 and I'm still getting autowire failures when running that say it can't find any matching beans:

                      Code:
                      Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type 
                      
                      [org.springframework.data.redis.core.ListOperations] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
                       
                      {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=redisTemplate)}
                      This isn't a big deal, I was more curious why it wasn't working and odds are I have something configured wrong, but trying to autowire this using 3.0.7 doesn't seem to work for me. I will probably attempt to setup a small project later with the latest 3.1 and see what that does.

                      thanks

                      Comment

                      Working...
                      X