spring 3.x整合ehcache 2.x
来源:互联网 发布:simnow python 编辑:程序博客网 时间:2024/06/11 07:30
1.pom.xml
<!-- spring 相关的jar--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring-version}</version> </dependency> <!-- https://mvnrepository.com/artifact/com.googlecode.ehcache-spring-annotations/ehcache-spring-annotations --> <dependency> <groupId>com.googlecode.ehcache-spring-annotations</groupId> <artifactId>ehcache-spring-annotations</artifactId> <version>1.2.0</version> </dependency> <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.2</version> </dependency>
2.applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!--other etc--> <!-- 缓存配置 --> <ehcache:annotation-driven /> <ehcache:config cache-manager="cacheManager"> <ehcache:evict-expired-elements interval="60" /> </ehcache:config> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml"/> </bean></beans>
3.ehcache.xml
<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true"> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false"/> <cache name="myCache" maxElementsOnDisk="20000" maxElementsInMemory="2000" eternal="true" overflowToDisk="true" diskPersistent="true"/> </ehcache>
4.代码:
import com.googlecode.ehcache.annotations.Cacheable;@Servicepublic class ChannelServiceImple implements ChannelService { @Cacheable(cacheName="myCache") @Override public List<ChannelCategory> getChannelCategories() {//Q5 // TODO Auto-generated method stub List<ChannelCategory> ccs=Collections.emptyList(); log.info("[apobates][cache start]service:getChannelCategories"); //ETC log.info("[apobates][cache finish]service:getChannelCategories"); return ccs; }}
5.看看log日志,缓存是否起作用了 ,
之前(没有ehcache):
[ INFO ] [apobates]user channel category [ INFO ] [apobates][cache start]service:getUserChannelCategories@by:1 [ INFO ] [apobates][cache finish]service:getUserChannelCategories@by:1 [ INFO ] [apobates]global channel category [ INFO ] [apobates][cache start]service:getChannelCategories [ INFO ] [apobates][cache finish]service:getChannelCategories [ INFO ] [apobates]user channel category [ INFO ] [apobates][cache start]service:getUserChannelCategories@by:1 [ INFO ] [apobates][cache finish]service:getUserChannelCategories@by:1 [ INFO ] [apobates]global channel category [ INFO ] [apobates][cache start]service:getChannelCategories [ INFO ] [apobates][cache finish]service:getChannelCategories [ INFO ] [apobates]user channel category [ INFO ] [apobates][cache start]service:getUserChannelCategories@by:1 [ INFO ] [apobates][cache finish]service:getUserChannelCategories@by:1 [ INFO ] [apobates]global channel category [ INFO ] [apobates][cache start]service:getChannelCategories [ INFO ] [apobates][cache finish]service:getChannelCategories [ INFO ] [apobates]user channel category [ INFO ] [apobates][cache start]service:getUserChannelCategories@by:1 [ INFO ] [apobates][cache finish]service:getUserChannelCategories@by:1 [ INFO ] [apobates]global channel category [ INFO ] [apobates][cache start]service:getChannelCategories [ INFO ] [apobates][cache finish]service:getChannelCategories
global channel category是spring mvc controller输出的
@Controller@Scope("prototype")@RequestMapping(value="/channel")@SessionAttributes({"channelForm"})public class ChannelController { @Autowired private ChannelService channelService; private static final Logger log=Logger.getLogger(ChannelController.class); //GLcategory @ModelAttribute public void addInitAttr(Model model,HttpServletRequest request){ log.info("[apobates]global channel category"); model.addAttribute("GLcategory",channelService.getChannelCategories()); }
当前():
[ INFO ] FrameworkServlet 'apobates_client': initialization completed in 213 ms [ INFO ] [apobates]user channel category [ INFO ] [apobates][cache start]service:getUserChannelCategories@by:1 [ INFO ] [apobates][cache finish]service:getUserChannelCategories@by:1 [ INFO ] [apobates]global channel category [ INFO ] [apobates][cache start]service:getChannelCategories [ INFO ] [apobates][cache finish]service:getChannelCategories [ INFO ] [apobates]user channel category [ INFO ] [apobates]global channel category [ INFO ] [apobates]user channel category [ INFO ] [apobates]global channel category [ INFO ] [apobates]user channel category [ INFO ] [apobates]global channel category [ INFO ] [apobates]user channel category [ INFO ] [apobates]global channel category [ INFO ] [apobates]user channel category [ INFO ] [apobates][cache start]service:getUserChannelCategories@by:19 [ INFO ] [apobates][cache finish]service:getUserChannelCategories@by:19 [ INFO ] [apobates]global channel category
0 0
- spring 3.x整合ehcache 2.x
- spring 4.x + mybatis 3.x 整合
- 基于Struts 2.x + Spring 2.x + Hibernate 3.x整合开发
- 基于Struts 1.x + Hibernate 3.x + Spring 2.x 整合
- spring4.x hibernate4.x 整合 ehcache 注解 annotate
- spring4.x hibernate4.x 整合 ehcache 注解 annotate
- SPRING4.X HIBERNATE4.X 整合 EHCACHE 注解 ANNOTATE
- spring4.x hibernate4.x 整合 ehcache 注解 annotate
- spring boot集成ehcache 2.x 用于hibernate二级缓存
- spring boot学习(十三)SpringBoot缓存(EhCache 2.x 篇)
- Spring 4.X整合Quartz2.X配置
- Spring 4.x+Spring MVC 4.x+MyBatis 3.x 整合(四)Spring 3.1.0 整合
- Spring与Struts1.x整合
- Dwr3.x spring整合配置
- struts 1.x spring 整合
- spring与struts1.x整合
- struts1.x和spring整合
- Spring 4.x+Spring MVC 4.x+MyBatis 3.x 整合(二)整合log4j日志工具
- wireshark : 明确connect、client-close、server-close、data-send
- vs+qt uic rcc 系统找不到指定的路径
- 微信小程序开发
- thinkphp控制器权限 (在构造函数时统一加权限等)
- android设置软键盘搜索键以及监听搜索键点击事件
- spring 3.x整合ehcache 2.x
- CyclicBarrier 源码阅读
- AndroidMainfest.xml详解——<permission-group>
- Spring Security——退出登录logout
- 2416,LED驱动分析
- 每天一个linux命令(44):top命令
- Spring 定时器注解 与配置文件
- Java位运算
- Java 常量的应用