使用Spring缓存的简单Demo
使用Spring缓存的简单Demo
1. 首先创建Maven工程,在Pom中配置
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
2. 创建Student类和StudentServer 类
package my.testcache; public class Student {
private String name;
private int age; public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
} }
StudentServer
package my.testcache; import java.util.HashMap;
import java.util.Map; import org.springframework.cache.annotation.Cacheable; public class StudentService { private Map<Integer, Student> stus = new HashMap<Integer, Student>();
{
stus.put(1, new Student("zhangsan",100));
stus.put(2, new Student("lisi", 106));
}; /**
* 根据参数对返回值进行缓存
*/
@Cacheable(value="students")
public Student getStudent(int id){
System.out.println("getStudent: " + id);
return stus.get(id);
};
}
注意:getStudent方法要定义为Public才能使用缓存。
有条件的缓存 condition = "#id < 2",表示只缓存id<2的数据。
@Cacheable(value="students", condition = "#id < 2")
public Student getStudent(int id){
System.out.println("getStudent: " + id);
return stus.get(id);
};
2. 在src/main/srsources中创建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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd"> <!-- 应用Bean类或者其方法的注解完成缓存的配置 -->
<cache:annotation-driven/>
<bean id="studentServer" class="my.testcache.StudentService" /> <!-- cacheManager使用JDK中的ConcurrentMap作为存储器 -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager" >
<property name="caches">
<set>
<bean id="students" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>
</set>
</property>
</bean>
</beans>
注意: 此时命名的bean students要与 StudentServer中的getStudents 的注解alue (@Cacheable(value="students"))一致
4. 执行Main方法
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); StudentService studentService = context.getBean(StudentService.class);
Student student1 = studentService.getStudent(1);
System.out.println(student1); Student student2 = studentService.getStudent(1);
System.out.println(student2);
}
5. 显示结果
getStudent: 1
Student [name=zhangsan, age=100]
Student [name=zhangsan, age=100]
说明第二次调用使用了缓存。
使用Spring缓存的简单Demo的更多相关文章
- Spring之AOP简单demo
1.加入JAR包.出了Spring自身的Jar包还要一些依赖的JAR包.不然会报ClassNotFound. Student.java package com.lubby.bean; import o ...
- Spring的简单demo
---------------------------------------- 开发一个Spring的简单Demo,具体的步骤如下: 1.构造一个maven项目 2.在maven项目的pom.xml ...
- Spring环境搭建及简单demo
1. Spring框架简介(以下这段话可用于面试求职) Spring为JavaEE开发提供了一个轻量级的解决方案,主要表现为, IOC(或者叫做DI)的核心机制,提供了bean工厂(Spring容器) ...
- 8 -- 深入使用Spring -- 5...1 启用Spring缓存
8.5.1 启用Spring缓存 Spring配置文件专门为缓存提供了一个cache:命名空间,为了启用Spring缓存,需要在配置文件中导入cache:命名空间. 导入cache:命名空间之后,启用 ...
- Spring缓存机制的理解
在spring缓存机制中,包括了两个方面的缓存操作:1.缓存某个方法返回的结果:2.在某个方法执行前或后清空缓存. 下面写两个类来模拟Spring的缓存机制: package com.sin90lzc ...
- 【Java EE 学习 78 上】【数据采集系统第十天】【Service使用Spring缓存模块】
一.需求分析 调查问卷中或许每一个单击动作都会引发大量的数据库访问,特别是在参与调查的过程中,只是单击“上一页”或者“下一页”的按钮就会引发大量的查询,必须对这种问题进行优化才行.使用缓存策略进行查询 ...
- Spring缓存框架原理浅谈
运维在上线,无聊写博客.最近看了下Spring的缓存框架,这里写一下 1.Spring 缓存框架 原理浅谈 2.Spring 缓存框架 注解使用说明 3.Spring 缓存配置 + Ehcache(默 ...
- Spring4.1新特性——Spring缓存框架增强(转)
目录 Spring4.1新特性——综述 Spring4.1新特性——Spring核心部分及其他 Spring4.1新特性——Spring缓存框架增强 Spring4.1新特性——异步调用和事件机制的异 ...
- Spring Boot项目简单上手+swagger配置+项目发布(可能是史上最详细的)
Spring Boot项目简单上手+swagger配置 1.项目实践 项目结构图 项目整体分为四部分:1.source code 2.sql-mapper 3.application.properti ...
随机推荐
- (转)整体把握jQuery -jQuery 的原型关系图
整体把握jQuery -jQuery 的原型关系图 (原)http://www.html5cn.org/article-6529-1.html 2014-7-2 17:12| 发布者: html5cn ...
- 关于jetty项目中的问题.
在某台虚拟机上部署的项目出现的问题: 我想要更改定义的owl文件,重启服务器,却打不开网页. 1.couldnot found owl ,然后我拷贝一份owl到work/config目录下,继续更改配 ...
- 使用Java代码实现对宽带的连接
对于多个类似的用户名相同的密码,运行java代码实现对宽带的自动连接 这是源代码: import java.io.BufferedReader; import java.io.IOException; ...
- RunLoop笔记
1.Runloop基础知识 - 1.1 字面意思 a 运行循环 b 跑圈 - 1.2 基本作用(作用重大) a 保持程序的持续运行(ios程序为什么能一直活着不会死) b 处理app中的各种事件(比如 ...
- WeView 里引用的H5中的文字 到行末尾 文字被切割
这个情况 在iPhone6以上没问题 以下有问题 具体情况是 我用以下代码计算内容的高度 NSString *injectionJSString = @"var script = doc ...
- [BZOJ 3223 & Tyvj 1729]文艺平衡树 & [CodeVS 3243]区间翻转
题目不说了,就是区间翻转 传送门:BZOJ 3223 和 CodeVS 3243 第一道题中是1~n的区间翻转,而第二道题对于每个1~n还有一个附加值 实际上两道题的思路是一样的,第二题把值对应到位置 ...
- Android RadioButton 语言无法切换问题
1.Dialog在不退出界面的情况下,RadioButton在语言切换时,无法匹配系统语言的问题: 解决办法为:在RadioButton添加属性 android:saveEnabled="f ...
- jQuery学习总结
1:jQuery是什么 jQuery是继prototype之后又一个优秀的Javascript框架.它是轻量级的js库,兼容各种浏览器(IE 6.0+, FF 1.5+, Safari 2.0+, O ...
- tensorflow资料补充(很棒)
http://tensorfly.cn/tfdoc/get_started/introduction.html https://github.com/CreatCodeBuild/TensorFlow ...
- PPT设计宝典!十招教你做出拿得出手的PPT
据说上班用 excel 的比 word 的工资高,用 ppt 的比用 excel 的工资高.无论如何,在职场演讲汇报中,PPT 扮演着至关重要的角色. 在本文我们将用 10 个超级技巧来解决糟糕的演示 ...