java - Spring @Cacheable 忽略创建代理
问题描述
我对 Spring @Cacheable 注释有一些问题。创建缓存管理代理时忽略它。
接下来是我的情况...
我的团队将Spring Boot (2.0)用于我们的应用程序。对于某些服务,我们使用带有Ehcache 3的缓存。接下来是Maven模块的结构:
/
+- cache-lib
|
+- service-lib // depend on cache-lib
|
+- app-module // depend on service-lib
cache-lib是一个模块,它具有用于缓存通用配置的配置类:
@Slf4j
@Configuration
@EnableCaching
@EnableConfigurationProperties(CustomCacheProperties.class)
public class CommonCacheConfig implements JCacheManagerCustomizer {
@Autowired
private CustomCacheProperties cacheProperties;
@Override
public void customize(javax.cache.CacheManager cacheManager) {
addCachesFromConfiguration(cacheManager, cacheProperties);
}
...
}
在service-lib中有一个服务,它使用存储库对象来查询数据库。
@Slf4j
public class DataServiceImpl implements DataService {
private final DataRepository dataRepository;
public DataServiceImpl(DataRepository dataRepository) {
this.dataRepository = dataRepository;
}
@Override
@Cacheable("DATA_CACHE")
public Data getData() {
log.info("Reloading data...");
return dataRepository.getData();
}
}
它的配置:
@Configuration
@EntityScan(basePackageClasses = Data.class)
@EnableJpaRepositories(basePackageClasses = DataRepository.class)
@Import(CommonCacheConfig.class)
@PropertySource("classpath:cache/data-service-cache.properties")
public class DataServiceConfig {
@Bean
public DataService dataService(DataRepository dataRepository) {
return new DataServiceImpl(timeShiftRepository);
}
}
根据此页面,应创建具有@Cacheable注释代理的类(如@Transactional的情况)。但是,当我在app-module中调试DataService时,我找不到任何代理。Spring 将这个 bean 管理为 POJO。当我在应用程序的任何地方使用DataService时,它每次使用它都会调用存储库。例如:
@Slf4j
@SpringBootApplication
@Import(DataServiceConfig.class)
public class App implements CommandLineRunner {
@Autowired
private DataService dataService;
public static void main(String... args) {
SpringApplication.run(App.class, args);
}
@Override
public void run(String... args) throws Exception {
log.info("Data: {}", dataService.getData());
log.info("Data: {}", dataService.getData());
}
}
输出是:
...
2018-06-20 12:47:13.054 INFO 9805 --- [main] org.ehcache.core.EhcacheManager : Cache 'DATA_CACHE' created in Eh107InternalCacheManager.
...
2018-06-20 12:47:24.299 INFO 9805 --- [main] com.example.service.DataServiceImpl : Reloading data...
2018-06-20 12:47:24.300 INFO 9805 --- [main] com.example.App : Data: {...}
2018-06-20 12:47:24.302 INFO 9805 --- [main] com.example.service.DataServiceImpl : Reloading data...
2018-06-20 12:47:24.303 INFO 9805 --- [main] com.example.App : Data: {...}
我错过了什么?我想我应该在 bean 加载期间错过一些东西,但我不知道我应该寻找什么。
任何人都可以给我任何建议吗?
感谢您的时间。
解决方案
推荐阅读
- ios - TableView Cell内的选择tableView - swift 3中的didSelectRowAt
- sql - 在 Teradata Studio Express 中禁用括号的自动完成
- python - 使用python读取带有特殊字符的CSV
- android - 升级到Android studio Bumblebee canary 4 throw java.lang.AbstractMethodError
- powershell - 如何让 netcat 服务器处理来自 Powershell 的上传文件?
- javascript - 更改 dropzone.js 和 rails 的错误文本
- python - matplotlib中二维函数的粒子群
- sql - WPF 和 Windowformapps 中的数据库
- excel - 从单元格中分离文本
- windows - Powershell在计算机上搜索特定描述