您的位置:首页 > 编程语言 > Java开发

springmvc 结合 ehcache

2016-07-02 22:30 411 查看
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.csdn.net/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即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: