使用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 ...
随机推荐
- STM32 HAL固件库编程的文件构架
对于我这种以前只接触过51和AVR单片机编程的小菜来说,现在开始学习STM32的编程,对于函数的功能以及C语言的语法都还好理解,难的是它提供的那一套硬件抽象层(HAL)驱动是怎么和其他的东东搭配在一起 ...
- php7.0支持调用lua脚本
需求前瞻: 最近因需求,需要支持php调用现有的Lua代码,可以减少php的代码量,同时在维护上也比较方便,不会照成需求变更,需要同时修改两份代码 第一步:安装php7.0及以上版本,此处安装方法就略 ...
- 吐槽THINKPHP5命令行
thinkphp,作为国内开源框架,一直在使用和学习. 但是实在忍不住想要吐槽一下他的开发文档,和 对初学者的不友好,建议刚接触MVC思想的人 还是尽量去使用其他框架当入门. 现在来吐槽一下think ...
- 多线程随笔知识点总结-NSThread4.1
线程的状态 状态说明 a.新建 实例化线程对象 b.就绪 向线程对象发送start消息,线程对象被加入可调度线程池等待CPU调度;detach方法和performSelectorInBackGroun ...
- codeforces 723E (欧拉回路)
Problem One-Way Reform 题目大意 给一张n个点,m条边的无向图,要求给每条边定一个方向,使得最多的点入度等于出度,要求输出方案. 解题分析 最多点的数量就是入度为偶数的点. 将入 ...
- Amd64 and Va_arg
Made of Bugs Blog Archives Author Amd64 and Va_arg OCT 3RD, 2010 A while back, I was poking around L ...
- div内容滚动,无缝滚动
function startmarquee(lh, speed, delay) { var t; var oHeight = 373; /** div的高度 **/ ...
- HttpContext详解
HttpContext 类:封装有关个别 HTTP 请求的所有 HTTP 特定的信息. 在处理请求执行链的各个阶段中,会有一个对象在各个对象之间进行传递,也即会保存 请求的上下文信息,这个对象就是Ht ...
- ubuntu10.04.4下安装JDK
第一步:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html下载安装包(本来是可以通过 ...
- 转载:Android横屏竖屏切换的问题
一.禁止横竖屏转换 Android横竖屏切换在手机开发中比较常见,很多软件在开发过程中为了避免横竖屏切换时引发不必要的麻烦,通常禁止掉横竖屏的切换, 通过在AndroidManifest.xml中设置 ...