spring ehcache 多个maven module 中多个ehcache.xml 配置文件的处理方式

问题描述:

       多个maven module 下都使用了ehcache.xml 文件 ,导致会出现配置文件中的缓存块找不到异常。

问题前的配置:

<?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:cache="http://www.springframework.org/schema/cache"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/cache
     http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
  
    <!-- <ehcache:annotation-driven /> -->  
	<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
	<cache:annotation-driven cache-manager='ehcacheManager'/>
	 
	<!-- cacheManager工厂类,指定ehcache.xml的位置 -->
	<bean id='ehcacheManagerFactory' class='org.springframework.cache.ehcache.EhCacheManagerFactoryBean'>
		<property name='configLocation' value='classpath:config/ehcache.xml' />
		<property name="shared" value="true"/>
	</bean>
	
	<!-- 声明cacheManager -->
	<bean id='ehcacheManager' class='org.springframework.cache.ehcache.EhCacheCacheManager'>
		<property name='cacheManager' ref='ehcacheManagerFactory' />
	</bean> 
</beans>  

解决方式:

    将以上配置改为:(为每个CacheManagerName 取个名字)

<?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:cache="http://www.springframework.org/schema/cache"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/cache
     http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
  
    <!-- <ehcache:annotation-driven /> -->  
	<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
	<cache:annotation-driven cache-manager='ehcacheManager'/>
	 
	<!-- cacheManager工厂类,指定ehcache.xml的位置 -->
	<bean id='ehcacheManagerFactory' class='org.springframework.cache.ehcache.EhCacheManagerFactoryBean'>
		<property name='configLocation' value='classpath:config/ehcache.xml' />
		<!--多个配置文件导致后面的配置文件无法被加载,解决方法:去掉缓存共享配置,添加自身的缓存管理器名称  -->
		<property name="cacheManagerName" value="exbManager"/>
	</bean>
	
	<!-- 声明cacheManager -->
	<bean id='ehcacheManager' class='org.springframework.cache.ehcache.EhCacheCacheManager'>
		<property name='cacheManager' ref='ehcacheManagerFactory' />
	</bean> 
</beans>  
问题被完美解决!

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <name>Timo后台管理系统</name> <description>快速搭建后台管理的系统</description> <groupId>com.linln</groupId> <artifactId>timo</artifactId> <version>2.0.3</version> <packaging>pom</packaging> <modules> <module>admin</module> <module>common</module> <module>component</module> <module>devtools</module> <module>modules</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.7</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <!--shiro权限管理框架版本:component.shiro--> <shiro.version>1.9.0</shiro.version> <lombok.version>1.18.24</lombok.version> <ehcache.version>2.10.9.2</ehcache.version> <mysql.connector.version>5.1.46</mysql.connector.version> <jsoup.version>1.14.3</jsoup.version> <!--shiro模板视图权限标签扩展:component.thymeleaf--> <thymeleaf-shiro.version>2.1.0</thymeleaf-shiro.version> <!--excel工具框架版本:component.excel--> <poi.version>4.1.2</poi.version> <!--jwt处理框架版本:component.jwt--> <jwt.version>3.19.1</jwt.version> <!--swagger-knife4j接口文档页面:component.jwt--> <knife4j.version>3.0.3</knife4j.version> <google.findbugs.version>3.0.1</google.findbugs.version> <skipTests>true</skipTests> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <!--spring data jpa持久层框架--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!--mysql连接驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.connector.version}</version> <scope>runtime</scope> </dependency> <!--lombok语法糖--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <optional>true</optional> </dependency> <!--ehcache缓存框架--> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>${ehcache.version}</version> </dependency> <!--html解析工具,处理xss攻击--> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>${jsoup.version}</version> </dependency> <!--knife4j 接口文档--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>${knife4j.version}</version> </dependency> <!--解决编译时javax.annotation.meta.When不存在问题--> <dependency> <groupId>com.google.code.findbugs</groupId> <artifactId>annotations</artifactId> <version>${google.findbugs.version}</version> </dependency> </dependencies> <!--多环境配置--> <profiles> <!--开发环境--> <profile> <id>dev</id> <activation> <!--默认配置--> <activeByDefault>true</activeByDefault> </activation> <properties> <profile.name>dev</profile.name> </properties> </profile> <!--生产环境--> <profile> <id>prod</id> <properties> <profile.name>prod</profile.name> </properties> </profile> </profiles> </project>
06-28
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值