package com.ilucky.spring.cache;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ilucky.spring.cache.model.User;
import com.ilucky.spring.cache.service.UserService;
/**
* @author IluckySi
* @date 20140614
* 测试spring缓存机制是否生效.
*/
public class MainTest {
public static void main(String[] args) {
//加载ClassPath路径下的spring配置文件, 并获取service bean.
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-cache.xml");
UserService userService = context.getBean("userService", UserService.class);
System.out.println("service bean: " + userService);
//添加三个用户, 然后查询并输出.
userService.add(new User("1", "Ilucky1", "pwd1"));
userService.add(new User("2", "Ilucky2", "pwd2"));
userService.add(new User("3", "Ilucky3", "pwd3"));
Map<String, User> map = userService.getAll();
for(Entry<String, User> entry : map.entrySet()) {
System.out.println(entry.getValue());
}
//验证@Cacheable和CachePut的区别, CachePut既保证方法被调用,同时保证结果被缓存.
Map<String, User> map2 = userService.getAll();
for(Entry<String, User> entry : map2.entrySet()) {
System.out.println(entry.getValue());
}
}
}
/**
spring缓存分为两种:方法和数据,如果方法被清除了,需要重新从数据库获取数据放入缓存,另外只有方法加入了缓存才可以获取缓存中的数据.
@Cacheable(value = "data")
public Map<String, User> getAll() {
输出结果:
service bean: com.ilucky.spring.cache.service.impl.UserServiceImpl@17ec9f7
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method-getAll()
UserDao method- getAll()
记录日志--调用接口等操作!
3-Ilucky3-pwd3
2-Ilucky2-pwd2
1-Ilucky1-pwd1
3-Ilucky3-pwd3
2-Ilucky2-pwd2
1-Ilucky1-pwd1
@CachePut(value = "data")
service bean: com.ilucky.spring.cache.service.impl.UserServiceImpl@fd918a
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method-getAll()
UserDao method- getAll()
记录日志--调用接口等操作!
3-Ilucky3-pwd3
2-Ilucky2-pwd2
1-Ilucky1-pwd1
UserService: method-getAll()
UserDao method- getAll()
记录日志--调用接口等操作!
3-Ilucky3-pwd3
2-Ilucky2-pwd2
1-Ilucky1-pwd1
*/
没有合适的资源?快使用搜索试试~ 我知道了~
spring缓存机制-@CachePut的用法(四)
共17个文件
class:6个
java:6个
xml:3个
5星 · 超过95%的资源 需积分: 50 52 下载量 77 浏览量
2014-06-15
14:15:17
上传
评论
收藏 19KB RAR 举报
温馨提示
Spring缓存@CachePut注释的使用:既保证方法执行,又保证方法返回的数据放入缓存。
资源推荐
资源详情
资源评论
收起资源包目录
spring缓存机制-@CachePut的用法(四).rar (17个子文件)
spring缓存机制-@CachePut的用法(四)
spring-cache
pom.xml 834B
target
test-classes
classes
spring-cache.xml 1KB
com
ilucky
spring
cache
dao
UserDao.class 444B
impl
UserDaoImpl.class 2KB
MainTest.class 2KB
model
User.class 1KB
service
UserService.class 456B
impl
UserServiceImpl.class 2KB
src
test
java
main
resources
spring-cache.xml 1KB
java
com
ilucky
spring
cache
dao
UserDao.java 415B
impl
UserDaoImpl.java 1KB
model
User.java 818B
MainTest.java 3KB
service
UserService.java 423B
impl
UserServiceImpl.java 2KB
.project 461B
.classpath 3KB
共 17 条
- 1
资源评论
- white6353265242017-07-27可以用,不过不是我要的~
- btweibin2018-09-27顶一个。。。
IluckySi
- 粉丝: 70
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- C#,煎饼排序问题(Pancake Sorting Problem)算法与源代码
- C#,排列组合的堆生成法(Heap’s Algorithm for generating permutations)算法与源代码
- C#,老鼠迷宫问题的回溯法求解(Rat in a Maze)算法与源代码
- 6693eeb8d683458a07938615fba9e68f.apk
- C#,数值计算,解微分方程的龙格-库塔二阶方法与源代码
- C#,数值计算,用割线法(Secant Method)求方程根的算法与源代码
- C#,子集和问题(Subset Sum Problem)的算法与源代码
- mongodb 数据库基本操作
- Linux操作系统基础教程
- Linux操作系统相关习题集
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功