纳闷的错误:IllegalAccessError,EhcacheManager

本文探讨了在使用JUnit进行Spring集成测试时遇到的IllegalAccessError问题,特别是当尝试使用ehcache 1.1版本时,由于CacheManager构造方法的访问级别问题导致的错误,并提供了解决方案。
junit测试时在 加载spring bean的时候报错:
nested exception is java.lang.IllegalAccessError: tried to access method net.sf.ehcache.CacheManager.<init>(Ljava/io/InputStream;)V from class org.springframework.cache.ehcache.EhCacheManagerFactoryBean

发现是ehcache 1.1中没法使用independent cache,spring的javadoc说:
Set whether the EHCache CacheManager should be shared (as a singleton at the VM level) or independent (typically local within the application). Default is "false", creating an independent instance.

Note that independent CacheManager instances are only available on EHCache 1.2 and higher. Switch this flag to "true" if you intend to run against an EHCache 1.1 jar.

EhCacheManagerFactoryBean的代码如下:
[code]
// Independent CacheManager instance (the default).
if (this.configLocation != null) {
this.cacheManager = new CacheManager(this.configLocation.getInputStream());
}
else {
this.cacheManager = new CacheManager();
}
[/code]
在ehcache1.1中CacheManager()和,CacheManager(InputStream)都是private的,所以会发生
IllegalAccessError,但是为什么报错的是试图调用 init方法呢? init方法在1.2才出现的,1.1根本没这个方法。
在使用PageHelper进行分页查询时,可能会遇到`java.lang.IllegalAccessError`错误。这个错误通常是由于类加载器在加载类时遇到了访问权限问题。以下是一些可能的原因和解决方法: ### 可能的原因 1. **版本不兼容**:PageHelper版本与Spring或MyBatis版本不兼容。 2. **类路径冲突**:项目中存在多个版本的PageHelper或相关依赖,导致类加载冲突。 3. **访问权限问题**:某些类或方法被错误地声明为`private`或`final`,导致无法访问。 ### 解决方法 1. **检查版本兼容性**: - 确保PageHelper版本与Spring和MyBatis版本兼容。可以参考PageHelper的官方文档或GitHub页面查看兼容版本。 2. **排除冲突依赖**: - 使用Maven或Gradle检查项目中是否存在多个版本的PageHelper或其他相关依赖。可以使用以下命令查看依赖树: ```bash mvn dependency:tree ``` 或 ```bash gradle dependencies ``` - 在`pom.xml`或`build.gradle`中排除重复的依赖。 3. **检查类访问权限**: - 仔细检查代码中是否有类或方法被错误地声明为`private`或`final`。确保所有必要的类和方法都具有适当的访问权限。 4. **清理和重建项目**: - 有时,类加载器可能会缓存旧的类文件。尝试清理项目并重新构建: ```bash mvn clean install ``` 或 ```bash gradle clean build ``` 5. **更新PageHelper**: - 确保使用的是最新版本的PageHelper。可以通过Maven Central或PageHelper的GitHub页面查看最新版本。 ### 示例 假设你在使用Maven,并且项目中存在版本冲突,可以按照以下步骤解决: 1. 在`pom.xml`中找到PageHelper依赖: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.6</version> </dependency> ``` 2. 使用Maven命令查看依赖树: ```bash mvn dependency:tree ``` 3. 如果发现存在多个版本的PageHelper依赖,可以在`pom.xml`中排除重复的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </exclusion> </exclusions> </dependency> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值