問題因人而異,此處僅做備忘
整合過程:
1、添加maven依賴
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.6</version> </dependency>
2、開啟springboot緩存
@EnableCaching @SpringBootApplication public class EhcachetestApplication { public static void main(String[] args) { SpringApplication.run(EhcachetestApplication.class, args); } }
3、在使用位置注入 org.springframework.cache.CacheManager
注:上面飄紅並不是報錯的原因,可忽略。
4、打包測試,報 required a bean of type 'org.springframework.cache.CacheManager' that could not be found
5、增加 spring-boot-starter-cache 依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
6、再次打包測試,一切OK
問題原因分析:
spring-boot-starter-cache 是springboot緩存功能的依賴,如果要開啟springboot cache必需添加該依賴。這次因為這個地方忽視,造成調錯花費了不少時間,記錄下來加深記憶!