使用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 ...
随机推荐
- html学习心得
注释:浏览器会自动地在段落的前后添加空行.(<p> 是块级元素) 提示:使用空的段落标记 <p></p> 去插入一个空行是个坏习惯.用 <br /> 标 ...
- 13个JavaScript图表(JS图表)图形绘制插件【转】
现在网络上又有越来越多的免费的(JS 图表)JavaScript图表图形绘制插件.我之前给一家网站做过复杂的图形,我们用的是 highchart.在那段时间,没有很多可供选择的插件.但现在不同了,很容 ...
- redhat6.5修改yum为163源
把需要的安装包和配置文件打包,将命令整合到sh文件中,下载后解压运行yun_config.sh 即可 下载链接
- Java的CLASSPATH,趁还没忘赶紧写点
咳咳,睡眠不足加上年龄增长,真的赶脚记忆力不行啦. 接触Java以来,对于环境配置就是按照网上的教程,一路复制粘贴,也没啥想法; 最近决定啃啃ThinkInJava,没看两章就看到这CLASSPATH ...
- Sql Server 之 for xml (path,raw,auto,root)
1.for xml path('str') select ID,CreateTime from dbo.ArticleInfo for xml Path('mytitle') 结果:(注意:如果是s ...
- 如何让iOS 保持界面流畅?这些技巧你知道吗
如何让iOS 保持界面流畅?这些技巧你知道吗 作者:ibireme这篇文章会非常详细的分析 iOS 界面构建中的各种性能问题以及对应的解决思路,同时给出一个开源的微博列表实现,通过实际的代码展示如 ...
- 收拾那掉了一地的session
多个页面有如下多个session,本来可能是如下面这样的 Session["UId"] = 10; Session["UName"] = "test& ...
- java文件编译及运行
1 配置环境变量 使用鼠标右击“我的电脑”->属性->高级->环境变量 系统变量->新建->变量名:JAVA_HOME 变量值:C:\Program Files (x86 ...
- 【海洋女神原创】How to: Installshield做安装包时如何添加文件
我一直以为这不是一个问题,可是没想到在几个群内,对于如何向安装包添加文件不解的大有人在,今日稍暇,整理成篇,以供参考 首先我想再大声地说一遍:不要再跟我说英文看不懂了!!!!你做了程序员这一行,就得逼 ...
- 用 pytube 爬取 youtube 视频
这个方法比直接用浏览器插件逼格高点 1. 简介 需要用到 pytube 这个第三方库:https://github.com/nficano/pytube 这里只是把这个页面捡重要部分翻译了一下. py ...