springmvc 结合 ehcache

本文介绍如何配置EHCache缓存管理器并整合SLF4J与Log4j日志框架。通过示例展示了ehcache.xml配置文件的具体设置,包括缓存策略和磁盘存储路径等。同时,针对SLF4J与Log4j共存导致的日志绑定冲突问题给出了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ehcache 版本使用ehcache-2.10.2.jar


需要的包

ehcache-2.10.2.jar

slf4j-api-1.7.7.jar

slf4j-jdk14-1.7.7.jar


使用log4j日志框架需要的包

slf4j-log4j12-1.7.7.jar

log4j-1.2.17.jar

全部包,下载地址

http://download.youkuaiyun.com/detail/wangzhiqiang123456/9565929

配置问题文件

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">  
    <diskStore path="java.io.tmpdir/ehcache"/>  
    <defaultCache  
           maxElementsInMemory="1000"  
           eternal="false"  
           timeToIdleSeconds="120"  
           timeToLiveSeconds="120"  
           overflowToDisk="false"/>  
                  
    <cache name="menuCache"   
           maxElementsInMemory="1000"   
           eternal="false"  
           timeToIdleSeconds="120"  
           timeToLiveSeconds="120"  
           overflowToDisk="false"   
           memoryStoreEvictionPolicy="LRU"/>  
</ehcache>  



applicationContext.xml 只有关于ehcache的配置,其他配置没有贴出来

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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.2.xsd 
 http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
 http://www.springframework.org/schema/cache
 http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.2.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
 
<!--开启缓存注解-->
<cache:annotation-driven cache-manager="cacheManager"/>
      

<!-- 缓存配置开始 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
        <property name="configLocation" value="classpath:ehcache.xml" />  
    </bean>  
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">      
        <property name="cacheManager"  ref="cacheManagerFactory"/>      
    </bean>
<!-- 缓存配置结束 -->
    </beans>


测试方法

@Cacheable(value="menuCache")
public String getkey(String k)
{
log.debug("进来了");
return "55985985555";
}


log4j.properties

#定义根日志级别和输出端(定义了两个输出端) 
#log4j.rootLogger=DEBUG,CONSOLE,D,E  
log4j.rootLogger=DEBUG,CONSOLE
#定义第一个输出端,输出到控制台  
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout  
log4j.appender.CONSOLE.layout.ConversionPattern=%d [%t] %-5p %c(%L) - %m%n  
### 输出到日志文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n


### 保存异常信息到单独文件 ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = logs/error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
  
#定义具体某个包下的日志输出级别   
log4j.logger.org.springframework=ERROR


slf4j+log4j出现的问题

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [zip:D:/Oracle/Middleware/user_projects/domains/base_qumancheng/servers/AdminServer/tmp/_WL_user/_appsdir_ainmeweb_dir/e5r7ej/war/WEB-INF/lib/slf4j-jdk14-1.7.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [zip:D:/Oracle/Middleware/user_projects/domains/base_qumancheng/servers/AdminServer/tmp/_WL_user/_appsdir_ainmeweb_dir/e5r7ej/war/WEB-INF/lib/slf4j-log4j12-1.7.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.JDK14LoggerFactory]


错误提示,在两个包中存在相同的StaticLoggerBinder.class 删除一个class即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值