j2cache初始化

使用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

 

posted on 2020-03-12 17:07  webjlwang  阅读(853)  评论(0编辑  收藏  举报

导航