使用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]
说明第二次调用使用了缓存。
作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!