【问题标题】:Spring Data Redis 1.7.2.RELEASE, Injection RedisOperations instead of RedisTemplateSpring Data Redis 1.7.2.RELEASE,注入RedisOperations而不是RedisTemplate
【发布时间】:2016-09-28 13:04:03
【问题描述】:

在将 Spring Data Redis 1.5.2.RELEASE 迁移到 1.7.2.RELEASE 时遇到问题,因为 RedisCacheManager 在 1.5 中使用 RedisTemplate .2.RELEASE 和 1.7.2.RELEASE 中的 RedisOperations 作为构造函数参数。以下是我当前的 Spring Data Redis 配置,截至 1.5.2.RELEASE。我应该如何为 1.7.2.RELEASE 配置它?

<cache:annotation-driven cache-manager="cacheManager" />

    <!-- Redis Connection Factory -->
    <beans:bean id="jedisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
        p:host-name="${redis.host-name}" p:port="${redis.port}" p:use-pool="true" />

    <!-- Redis Template Definition -->
    <beans:bean id="redisTemplate"
        class="org.springframework.data.redis.core.RedisTemplate"
        p:connection-factory-ref="jedisConnectionFactory" p:keySerializer-ref="stringRedisSerializer"
        p:hashKeySerializer-ref="stringRedisSerializer" />

    <beans:bean id="stringRedisSerializer"
        class="org.springframework.data.redis.serializer.StringRedisSerializer" />

    <!-- declare Redis Cache Manager -->
    <beans:bean id='cacheManager'
        class='org.springframework.data.redis.cache.RedisCacheManager'
        c:template-ref='redisTemplate'>
        <beans:property name="expires">
            <beans:map>
                <beans:entry key="sessionData" value="7200"></beans:entry>
                <beans:entry key="portalData" value="7200"></beans:entry>
                <beans:entry key="referenceData" value="86400"></beans:entry>
            </beans:map>
        </beans:property>
    </beans:bean> 

如果我为 RedisOperations 声明一个 bean 并将其注入 RedisCacheManager 如下:

<beans:bean id="**redisOperations**"
        class="org.springframework.data.redis.core.RedisOperations"/>

<!-- declare Redis Cache Manager -->
        <beans:bean id='cacheManager'
            class='org.springframework.data.redis.cache.RedisCacheManager'
            c:template-ref='**redisOperations**'>
            <beans:property name="expires">
                <beans:map>
                    <beans:entry key="sessionData" value="7200"></beans:entry>
                    <beans:entry key="portalData" value="7200"></beans:entry>
                    <beans:entry key="referenceData" value="86400"></beans:entry>
                </beans:map>
            </beans:property>
        </beans:bean>

RedisCacheManager 已更改为在 1.7.2.RELEASE 中使用 RedisOperations,而不是在 1.5.2.RELEASE 中使用的 RedisTemplate。来源参考如下:

https://github.com/spring-projects/spring-data-redis/blob/1.7.2.RELEASE/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java

https://github.com/spring-projects/spring-data-redis/blob/1.5.2.RELEASE/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java

因为上面提到的redis配置让我出错

 ERROR org.springframework.web.servlet.DispatcherServlet localhost-startStop-1     - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.interceptor.CacheInterceptor#0': Cannot resolve reference to bean 'cacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cacheManager' defined in class path resource [spring/config/shared-config.xml]: Unsatisfied dependency expressed through constructor parameter 0: Ambiguous argument values for parameter of type [org.springframework.data.redis.core.RedisOperations] - did you specify the correct bean references as arguments?
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cacheManager' defined in class path resource [spring/config/shared-config.xml]: Unsatisfied dependency expressed through constructor parameter 0: Ambiguous argument values for parameter of type [org.springframework.data.redis.core.RedisOperations] - did you specify the correct bean references as arguments?
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1282)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1195)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1085)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5318)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5610)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:679)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1966)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cacheManager' defined in class path resource [spring/config/shared-config.xml]: Unsatisfied dependency expressed through constructor parameter 0: Ambiguous argument values for parameter of type [org.springframework.data.redis.core.RedisOperations] - did you specify the correct bean references as arguments?
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:736)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
    ... 35 more

我使用的是 Spring 4.3.2.RELEASE 和 Spring Data Redis 1.7.2.RELEASE。 请对此注入问题以及如何解决它提出您的建议。我不想在我的应用程序中使用集群设置。

【问题讨论】:

  • 您遇到了什么问题?您可以添加堆栈跟踪或您看到的任何错误吗?
  • @Reddy 我通过添加错误信息更新了问题。希望现在你能帮助我

标签: java spring spring-mvc spring-data spring-data-redis


【解决方案1】:

这应该可行。您只需要使用正确的构造函数名称空间。

<beans:bean id='cacheManager'
        class='org.springframework.data.redis.cache.RedisCacheManager'
        c:redis-operations-ref='redisTemplate'>
        <beans:property name="expires">
            <beans:map>
                <beans:entry key="sessionData" value="7200"></beans:entry>
                <beans:entry key="portalData" value="7200"></beans:entry>
                <beans:entry key="referenceData" value="86400"></beans:entry>
            </beans:map>
        </beans:property>
</beans:bean>

【讨论】:

  • redisOperations bean 怎么样?我们需要为它做一个声明吗?
  • 更新了答案。你可以只引用现有的模板 bean
  • 此配置工作正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-27
  • 1970-01-01
  • 2019-04-22
  • 2016-04-17
  • 2021-09-13
  • 1970-01-01
相关资源
最近更新 更多