如何解决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')),
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。