8 -- 深入使用Spring -- 5...2 使用@Cacheable执行缓存
8.5.2 使用@Cacheable执行缓存 @Cacheable可用于修饰类或修饰方法,当使用@Cacheable修饰类时,用于告诉Spring在类级别上进行缓存 ------ 程序调用该类的实例的任何方法时都需要缓存,而且共享同一个缓存区;当使用@Cacheable修饰方法时,用于告诉Spring在方法级别上进行缓存 ------ 只有当程序调用该方法时才需要缓存。 1. 类级别的缓存 当使用@Cacheable修饰类时,就可控制Spring在类级别进行缓存,这样程序调用类的任意方法时,只要传入的参数相同,Spring就会使用缓存。 Component : UserServiceImpl package edu.pri.lime._8_5_2.classrank.impl; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import edu.pri.lime._8_5_2.classrank.UserService; import edu.pri.lime._8_5_2.classrank.bean.User; @Service(value