XML cacheManger bean:多个CachingProviders错误

问题描述

您好,因此我曾经使用单个缓存提供程序来管理我的缓存,但是现在我必须使用多个缓存提供程序 而且我在初始化bean时遇到了一些问题,实际上我得到了这个错误

是否可以为我的XML bean定义特定的提供程序?

   Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jcacheManager' defined in class path resource [default-beans.xml]: Invocation of init method Failed; nested exception is javax.cache.CacheException: Multiple CachingProviders have been configured when only a single CachingProvider is expected

我的豆子

 <bean id="jcacheManager" class="org.springframework.cache.jcache.jcacheManagerfactorybean">
          <property name="cacheManagerUri" value="classpath:ehcache.xml"/>
        </bean>
        <bean id="DefaultCacheManager" class="org.springframework.cache.jcache.jcacheCacheManager" scope = "singleton">
          <property name="cacheManager" ref="jcacheManager" />
        </bean>

解决方法

如果要使用多个缓存提供程序,则应使用基于注释的缓存配置,这将减少您的工作!您需要做的只是:

启用基于注释的缓存配置。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/cache https://www.springframework.org/schema/cache/spring-cache.xsd">

        <cache:annotation-driven/> //This will allow you to use @Cache annotations.
</beans>

现在,您已在应用程序中启用了基于注释的缓存服务。


在一个类中,假设您有三个方法,并且希望将每个方法都缓存在不同的缓存提供程序中。

@Cacheable(cacheNames="name",cacheManager="yourProvider") 
public Object firstMethod(){
//You can enter different cache provider configured in each cache manager for each method.
}
,
path('',TemplateView.as_view(template_name='index.html')),

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...