Hibernate 5二级缓存配置异常求助:找不到org.slf4j.LoggerFactory
解决Hibernate 5二级缓存配置中的SLF4J ClassNotFoundException问题
首先明确一点:这个java.lang.ClassNotFoundException: org.slf4j.LoggerFactory异常不是Ehcache本身的问题,而是SLF4J核心API在运行时无法被找到导致的。结合你的依赖配置来看,大概率是依赖传递冲突或者构建环境的依赖加载问题,而非版本不兼容(你的SLF4J和Ehcache版本其实是兼容的)。
异常原因分析
org.slf4j.LoggerFactory类属于slf4j-api包,你已经在pom.xml中引入了这个依赖,但运行时找不到,可能的场景包括:
- 其他依赖通过
<exclusion>规则排除了slf4j-api - Maven依赖树中存在版本冲突,导致正确版本的
slf4j-api没有被引入到运行环境 - IDE的依赖缓存异常,没有正确加载
slf4j-api包
快速解决步骤
1. 检查并修复依赖冲突
执行Maven命令查看完整依赖树,找出可能的冲突:
mvn dependency:tree
在输出中搜索slf4j-api:
- 如果发现多个版本的
slf4j-api被引入,找到非1.7.25版本的来源,在对应的依赖中添加排除规则:
<dependency> <!-- 替换为实际存在冲突的依赖 --> <groupId>xxx</groupId> <artifactId>xxx</artifactId> <version>xxx</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> </exclusions> </dependency>
- 如果发现
slf4j-api被排除,直接移除对应的排除规则即可。
2. 确保依赖范围正确
虽然Maven默认依赖范围是compile,但可以明确指定slf4j-api的范围,避免意外:
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> <scope>compile</scope> </dependency>
3. 刷新IDE依赖缓存
如果是开发环境中出现的问题,执行IDE的Maven刷新操作:
- IntelliJ: 右键项目 →
Maven→Reload All Maven Projects - Eclipse: 右键项目 →
Maven→Update Project...
补全Hibernate 5二级缓存的正确配置
既然你本来是要配置二级缓存,这里给你一套完整的配置步骤,确保缓存正常工作:
1. Hibernate核心配置(hibernate.cfg.xml)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 其他数据库连接配置... --> <!-- 开启二级缓存 --> <property name="hibernate.cache.use_second_level_cache">true</property> <!-- 指定JCache作为缓存提供者 --> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.jcache.JCacheRegionFactory</property> <!-- 指向Ehcache的配置文件 --> <property name="hibernate.jcache.provider_configuration_resource_path">ehcache.xml</property> <!-- 可选:开启查询缓存 --> <property name="hibernate.cache.use_query_cache">true</property> </session-factory> </hibernate-configuration>
2. Ehcache 3.x配置文件(ehcache.xml,放在src/main/resources下)
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ehcache.org/v3" xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd"> <!-- 持久化缓存目录 --> <persistence directory="${java.io.tmpdir}/ehcache"/> <!-- 为你的实体类配置缓存区域 --> <cache alias="com.yourpackage.YourEntity"> <key-type>java.lang.Long</key-type> <value-type>com.yourpackage.YourEntity</value-type> <!-- 缓存过期时间 --> <expiry> <ttl unit="minutes">10</ttl> </expiry> <!-- 缓存资源配置 --> <resources> <heap unit="entries">1000</heap> <offheap unit="MB">100</offheap> </resources> </cache> </config>
3. 实体类开启缓存
在需要缓存的实体类上添加Hibernate缓存注解:
import javax.persistence.Entity; import javax.persistence.Id; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; @Entity @Cacheable @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "com.yourpackage.YourEntity") public class YourEntity { @Id private Long id; // 其他字段和方法... }
内容的提问来源于stack exchange,提问作者Mariia
相关产品推荐
相关产品推荐

