ehcache + spring 整合以及配置说明 ,附带整合问题 (已解决)
新做的项目,因为流量不大 就是一个征信平台,高峰流量不多,但缓存是必须的,cache到server上就可以,不需要额外的memcache、redis之类的东西。
但是遇到一个大坑,事情是这样的:
通过阅读大量教程,官方文档所知,该缓存框架是java进程内的缓存,开发便捷,缺点就是java kill掉后缓存就消失了,不会想其他缓存框架可以独立于JAVA程序外。
总结起来就是以下几点,特此记录
1:我配置好ehcache缓存框架,如下所示:重点就是cache name
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="es"> <diskStore path="java.io.tmpdir"/>
<!--1 timeToIdleSeconds ,多长时间不访问该缓存,那么ehcache 就会清除该缓存. 2 timeToLiveSeconds ,缓存的存活时间,从开始创建的时间算起. 3 eternal 缓存是否持久 4 overflowToDisk 是否保存到磁盘,当系统宕机时 --> <defaultCache
maxEntriesLocalHeap="1000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
overflowToDisk="false">
</defaultCache> <cache name="cisReportRoot"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="600"
timeToLiveSeconds="600"
overflowToDisk="false"
statistics="true">
</cache> <cache name="user"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="600"
timeToLiveSeconds="600"
overflowToDisk="false"
statistics="true">
</cache> </ehcache>
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:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <context:component-scan base-package="com.xxxx.credit.*"/>
<cache:annotation-driven/>
<!-- 使用全注释事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/> <!-- cacheManager, 指定ehcache.xml的位置 -->
<bean id="ehcacheManager" 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="ehcacheManager"/>
<property name="transactionAware" value="true"/>
</bean> <bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean> <!-- 数据源配置, 使用应用中的DBCP数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/> <!-- Connection Pooling Info -->
<property name="maxActive" value="${dbcp.maxActive}"/>
<property name="maxIdle" value="${dbcp.maxIdle}"/>
<property name="defaultAutoCommit" value="false"/> <!-- 连接Idle一个小时后超时 -->
<property name="timeBetweenEvictionRunsMillis" value="3600000"/>
<property name="minEvictableIdleTimeMillis" value="3600000"/>
</bean> <!-- spring和MyBatis整合 -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 自动扫描Mapper.xml文件 -->
<property name="mapperLocations" value="classpath:mybatis/*Mapper.xml"/>
</bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.pingan.credit.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean> <!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 懒加载 -->
<bean id="jacksonObjectMapper" class="com.pingan.credit.model.MyJsonMapper" /> </beans>
4:然后写一个简单的bean,service 做测试验证配置是否正确:
Bean
package com.pingan.credit.model; import java.io.Serializable; public class User implements Serializable {
private Long id;
private String username;
private String email; public User() {
}
public User(Long id, String username, String email){
this.id = id;
this.username = username;
this.email = email;
} public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (id != null ? !id.equals(user.id) : user.id != null) return false; return true;
} @Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", email='" + email + '\'' +
'}';
}
}
Service:
package com.xxxx.credit.service; import com.xxxx.credit.model.User;
import org.apache.log4j.Logger;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import java.util.HashSet;
import java.util.Set; @Service("userService")
public class UserService { private static Logger logger = Logger.getLogger(UserService.class);
Set<User> users = new HashSet<User>(); @CachePut(value = "user", key = "#user.id")
public User save(User user) {
users.add(user);
logger.info(user.toString());
return user;
} @CachePut(value = "user", key = "#user.id")
public User update(User user) {
users.remove(user);
users.add(user);
return user;
} @CacheEvict(value = "user", key = "#user.id")
public User delete(User user) {
users.remove(user);
return user;
} @CacheEvict(value = "user", allEntries = true)
public void deleteAll() {
users.clear();
} @Cacheable(value = "user", key = "#id")
public User findById(final Long id) {
System.out.println("cache miss, invoke find by id, id:" + id);
for (User user : users) {
if (user.getId().equals(id)) {
return user;
}
}
return null;
} }
然后开始测试:
@Test
public void testCacheUser() {
System.out.println("test cache()");
Long id = 1L;
User user = new User(id, "zhang333", "zhang@gmail.com");
User save = userService.save(user);
junit.framework.TestCase.assertEquals(true,user==save);
System.out.println("123");
User result = userService.findById(id);
}
通过验证: 缓存正常输出 ,也理解通过UserService中@Cacheable注解后,不会进入方法体内,也就是说不会有sysout.out.println()这句输出
直接返回save方法中 save() 方法return的值
也就是说 save方法 会缓存 return的那个user
好了 道理都懂了 junit也测过了 差不许多可以开始上测试服务器了
这个时候,折磨了我4个小时的问题来了,
我在本地一切问题都没有,但是通过post man(http 请求工具)就是拿不到缓存数据,当时总觉得是缓存框架配置的有问题,但恰恰并不是
原因在这:spring-mvc.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--自动扫描,注解控制器 -->
<context:component-scan base-package="com.xxxx.credit.*"/> <!--注解驱动 -->
<mvc:annotation-driven/> <!--静态资源映射-->
<!--本项目把静态资源放在了WEB-INF的statics目录下,资源映射如下-->
<mvc:resources mapping="/css/**" location="/WEB-INF/statics/css/"/>
<mvc:resources mapping="/js/**" location="/WEB-INF/statics/js/"/>
<mvc:resources mapping="/image/**" location="/WEB-INF/statics/image/"/> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀(如果最后一个还是表示文件夹,则最后的斜杠不要漏了) 使用JSP-->
<!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/><!--设置JSP文件的目录位置-->
<property name="suffix" value=".jsp"/>
</bean> <!-- springMvc文件上传需要配置的节点-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="20971500"/><!--设置JSP文件的目录位置-->
<property name="defaultEncoding" value="UTF-8"/><!--设置默认编码-->
<property name="resolveLazily" value="true"/><!--启用是为了推迟文件解析,以便捕获文件大小异常-->
</bean> </beans>
但是更改成:
<context:component-scan base-package="com.pingan.xxxx.controller"/>
就没有问题了,擦 折腾了我4个小时的问题终于找到了,魔鬼细节啊 魔鬼细节啊 魔鬼细节啊 还是马虎造成的,如次简单的问题又耗费了大量的时间
谨记
ehcache + spring 整合以及配置说明 ,附带整合问题 (已解决)的更多相关文章
- spring与mybatis三种整合方法
spring与mybatis三种整合方法 本文主要介绍Spring与Mybatis三种常用整合方法,需要的整合架包是mybatis-spring.jar,可通过链接 http://code.googl ...
- spring boot与jdbcTemplate的整合案例2
简单入门了spring boot后,接下来写写跟数据库打交道的案例.博文采用spring的jdbcTemplate工具类与数据库打交道. 下面是搭建的springbootJDBC的项目的总体架构图: ...
- 【Spring】17、spring cache 与redis缓存整合
spring cache,基本能够满足一般应用对缓存的需求,但现实总是很复杂,当你的用户量上去或者性能跟不上,总需要进行扩展,这个时候你或许对其提供的内存缓存不满意了,因为其不支持高可用性,也不具备持 ...
- spring学习七 spring和dynamic project进行整合
spring和web项目进行整合,其实就是在项目启动时,就创建spring容器,然后在servlet中使用spring容器进行开. 注意:为了页面可以访问到servlet,因此servlet必须放进t ...
- SSM框架整合的详细过程(含每一步的分析及代码)。实质上是SpringMVC与mybatis的整合,应为spring与SpringMVC不需要整合。
为了更好的学习 springmvc和mybatis整合开发的方法,需要将springmvc和mybatis进行整合. 整合目标:控制层采用springmvc.持久层使用mybatis实现. 1.1 需 ...
- Spring入门(四)— 整合Struts和Hibernate
一.Spring整合Struts 1. 初步整合 只要在项目里面体现spring和 strut即可,不做任何的优化. struts 环境搭建 创建action public class UserAct ...
- spring与mybatis五种整合方法
1.采用数据映射器(MapperFactoryBean)的方式 不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数. (1)Spring配置文件: <!-- 引入jdbc ...
- Spring Boot 中使用 MyBatis 整合 Druid 多数据源
2017 年 10 月 20 日 Spring Boot 中使用 MyBatis 整合 Druid 多数据源 本文将讲述 spring boot + mybatis + druid 多数据源配置方 ...
- Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合
Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合 标签: mybatisSpringbeanApplicationContextMapper 2015-12-31 1 ...
- Spring+SpringMVC+MyBatis+Maven框架整合
本文记录了Spring+SpringMVC+MyBatis+Maven框架整合的记录,主要记录以下几点 一.Maven需要引入的jar包 二.Spring与SpringMVC的配置分离 三.Sprin ...
随机推荐
- Codeforces 758A Holiday Of Equality
题目链接:http://codeforces.com/problemset/problem/758/A A. Holiday Of Equality time limit per test 1 sec ...
- 字符串反转,栈模拟(ZOJ1151)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=151 这里可以用栈模拟,也可以用STL,reverse();函数. 但 ...
- 离散外微积分(DEC:Discrete Exterior Calculus)基础
原文链接 “若人们不相信数学简单,只因为他们未意识到生命之复杂.”——Johnvon Neumann DEC主要讨论离散情况下的外积分,它在计算机领域有重要用途.我们知道,使用计算机来处理几何图形的时 ...
- jeDate日期控件
http://www.jayui.com/jedate/ 这是日期控件官网,可以去里面下载使用 前台 <%@ Page Language="C#" AutoEvent ...
- EBS补丁时遇到的问题
今天在给R12.1.3打17525552的时候,出现了这样的一个错误 ATTENTION: All workers either have failed or are waiting: FAILED: ...
- 学习JavaScript一些资料时,记录一些规范小记
最近工作不是很忙,所以再深入学学JavaScript,顺便提升一下自己,嘿嘿!主要记录一下学习到的一下编写代码的规范小记吧! 1.声明变量时一定带上var,避免一些错误发生,如变量提升时遇见的问题发生 ...
- 微信小程序日期选择器
/* JS代码部分 */ const date = new Date() const years = [] const months = [] const days = [] const hours ...
- LeetCode297. Serialize and Deserialize Binary Tree
题目 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据. 请设计一个算法来实 ...
- webkit几种内核版本的优劣对比总结
01.Open-Webkit-Sharp 默认不存在JS对话框,如果需要显示alert box,则需要在ShowJavaScriptAlertPanel进行捕捉,然后显示对话框: 02.Open-We ...
- (转)数据库老兵:NewSQL才是未来
编者按:在数据库技术领域,Michael Stonebraker几乎是无人不知无人不晓的人物.现年70岁的Stonebraker不仅是Ingres和PostgreSQL的创始人,同时在Informix ...