问题遇到的现象和发生背景
初次搭建springboot+ehcache项目,为方便运维,将所有配置文件都提取到了外层config下,但是测试时,ehcache不生效
问题相关代码,请勿粘贴截图
@EnableCaching
@SpringBootApplication
public class App
{
private static ApplicationContext context;
public static void main( String[] args )
{
context = new FileSystemXmlApplicationContext("E:\\eclipseworkspace\\Ehcache-01\\config\\applicationContext.xml");
SpringApplication.run(App.class, args);
}
/**
*
* @return
*/
public static ApplicationContext getContext() {
return context;
}
}
@RestController
@RequestMapping("hello2")
public class HelloController {
@Autowired
private HelloService helloService;
@RequestMapping("")
public String hello() {
// helloService = (HelloService) App.getContext().getBean("server");
return helloService.getTime();
}
}
@Service
public class HelloService {
@Cacheable(value="GoodsType")
public String getTime() {
return String.valueOf(System.currentTimeMillis());
}
}
<!-- 1、启用缓存注解开关, cache-manager 属性用来指定当前所使用的CacheManager对应的bean的名称,默认是cacheManager -->
<cache:annotation-driven cache-manager="springCacheManager"/>
<!-- 2、映射到ehcacheManager -->
<bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManager"/>
</bean>
<!-- 3、Ehcache实现, 对应 ehcache/ehcache.xml -->
<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<!-- 指定配置文件的位置 -->
<property name="configLocation" value="E:\eclipseworkspace\Ehcache-01\config\ehcache.xml"/>
</bean>
<!-- <bean id="server" class="com.zhangzhen.Ehcache_01.HelloService"></bean> -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap" />
</defaultCache>
<!-- 测试 -->
<cache name="GoodsType"
eternal="false"
timeToIdleSeconds="2"
timeToLiveSeconds="2"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU">
</cache>
运行结果及报错内容
我的解答思路和尝试过的方法
使用@Lazy+@autowired进行懒加载也同样无效
我想要达到的结果