使用j2cache時一時疏忽,用新的引入方法缺用了舊的編碼方式,導致j2cache比較奇怪的初始化兩次,第一次用的springboot指定的配置文件,第二次用的錯誤的默認配置文件。
maven pom.xml引入時:
<dependency> <groupId>net.oschina.j2cache</groupId> <artifactId>j2cache-spring-boot2-starter</artifactId> <version>2.7.2-release</version> </dependency>
此時,如果在java程序中通過j2cache函數初始化CacheChannel則會導致初始化兩次j2cache,導致使用了j2cache.jar中默認的配置文件,所以自己的配置就失效了,即下面這種寫法:
private static CacheChannel cache = J2Cache.getChannel();
因為通過springboot starter裝備的組件會通過spring初始化,直接用通過spring機制注入就可以了,不需要通過編碼構建:
@Component public class J2CacheUtils { @Autowired private CacheChannel cacheChannel; ...
然后就可以正常使用cacheChannel的方法了。
這篇博客介紹了Spring boot starter機制:https://blog.csdn.net/qq_35067322/article/details/99767580