缓存是一种存储机制,它将数据保存在某个地方,并以一种更快的方式为日后的请求提供服务,在应用程序中使用缓存机制,可以避免方法的多次执行,可以根据所提供的输入值对方法的结果数据进行缓存
通过使用AOP原则,对方法进行编织,如果已经为提供的参数执行过,那么不必执行实际方法就可以返回被缓存的结果,所以需要为应用程序的所有类生成代理类,并使用缓存注解对类的方法进行标记
(1)创建一个空Maven,添加以下依赖,缓存注金额之类的类位于spring-contextJAR中
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>4.1.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.1.6.RELEASEversion>
dependency>
(2)在com.wiley.beginningspring.ch10包中创建域类User
public class User {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
(3)在com.wiley.beginningspring.ch10包中创建
public class UserService {
private Map users=new HashMap();
{
users.put(1,new User(1,"Kenan"));
users.put(2,new User(2,"Mert"));
}
@Cacheable(value="users")//根据方法参数对方法的返回值进行缓存
public User getUser(int id)
{
System.out.println("id = [" + id + "]");
return users.get(id);
}
}
(4)在src/main/resources/文件中创建applicationContext.xml配置文件
<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.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven/>
<bean id="usersService" class="com.wiley.beginningspring.ch10.UserService">
bean>
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean id="users" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">bean>
set>
property>
bean>
beans>
(5)测试
public class Main {
public static void main(String[] args) {
ApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService=context.getBean(UserService.class);
User userFetch1=userService.getUser(1);// 第一次执行etUser方法返回
System.out.println(userFetch1);
User userFetch2=userService.getUser(2);
System.out.println(userFetch2);
User userFetch3=userService.getUser(1);// 第二次直接从缓存返回
System.out.println(userFetch3);
User userFetch4=userService.getUser(1);// 第三次也是直接从缓存返回
System.out.println(userFetch4);
}
}
<cache:annotation-driven cache-manager="myCacheManager"/>
<bean id="myCacheManager" class="org.springframework.cache.support.SimpleCacheManager">
</bean>
除了xml配置还可以使用注解配置
@Configuration
@ComponentScan(basePackages = {"com.wiley.beginningspring.ch10"})
@EnableCaching
public class ApplicationConfig {
@Bean
public CacheManager cacheManager()
{
//创建一个SimpleCacheManager并使用了名称users设置了新ConcurrentMapCache
SimpleCacheManager cacheManager=new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache(
"users"
)));
return cacheManager;
}
}
测试
public class Main {
public static void main(String[] args) {
// ApplicationContext context=
// new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext context=new AnnotationConfigApplicationContext(ApplicationConfig.class);
UserService userService=context.getBean(UserService.class);
User userFetch1=userService.getUser(1);// 第一次执行etUser方法返回
System.out.println(userFetch1);
User userFetch2=userService.getUser(2);
System.out.println(userFetch2);
User userFetch3=userService.getUser(1);// 第二次直接从缓存返回
System.out.println(userFetch3);
User userFetch4=userService.getUser(1);// 第三次也是直接从缓存返回
System.out.println(userFetch4);
}
}