当前位置: 首页 > 知识库问答 >
问题:

Spring Data Redis 1.7.2.Release、Injection RedisOperations而不是RedisTemplate

薛承基
2023-03-14

我在将Spring Data Redis1.5.2.Release迁移到1.7.2.Release时遇到问题,因为RedisCacheManager在1.5.2.Release中使用RedisTemplate和在1.7.2.Release中使用RedisOperations作为构造函数参数。下面是我当前的Spring数据Redis配置,截止到1.5.2.版本。我应该如何为1.7.2.版本配置它?

<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>

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版和Spring Data Redis 1.7.2版。请有您的建议,关于这个注射问题和如何解决它。我不想在应用程序中使用集群设置。

共有1个答案

韦绍晖
2023-03-14

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

<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>
 类似资料:
  • 我有一个现有的Android应用程序。我已经根据文档向项目添加了React NativeV0.30活动(除了我用替换了类)。在发展模式上,一切都很好。但是当我创建应用程序的发布版本时,看起来应用程序的React本机部分仍然处于调试模式,当我打开React本机驱动的活动时,我在logcat中得到以下错误消息: 更新: 顺便说一句,我在应用程序的gradle配置中的中有这样的内容:

  • 我有以下项目结构: 框架 框架父pom 构架某事 在框架-父母-pom的pom.xml中,我定义了以下插件: 并遵循SCM: 当我运行以下命令时。。。 ……一切似乎都很顺利。 在本地创建了带有发布版本的JAR,并且POM很好地更新到下一个SNAPSHOT版本。同样在SVN中,乍一看似乎还可以。标签已创建,其中包含所有框架项目。 但是,当查看标记的POM时,我看到它们仍然具有初始快照版本作为版本。这

  • 问题内容: 我正在使用postgreSQL。我有一列: 但是,当我想插入带有空字符串的行时,如下所示: 它不会给我错误并接受。如何检查插入值应为?(既不为空也不为空) PS: 我的专栏定义为: 问题答案: 向列定义添加约束。例如类似: 有关更多信息,请参见http://www.postgresql.org/docs/current/static/ddl- constraints.html

  • When run, this command line interface automatically generates a new GitHub Release and populates it with the changes (commits) made since the last release. Usage Firstly, install the package from npm

  • 8.0.0 Released on 5th of August, 2020Major changes This version introduces a completely new architecture for row and column management - index mapper, which is responsible for the storage and manageme

  • 0.24.4 (2014-08-09) pem file is used by mockserver and required by scrapy bench (commit 5eddc68) scrapy bench needs scrapy.tests* (commit d6cb999) 0.24.3 (2014-08-09) no need to waste travis-ci time o