NutzCN Logo
问答 ehcache获取缓存空指针
发布于 2539天前 作者 binfoo 2923 次浏览 复制 上一个帖子 下一个帖子
标签:

报错日志

Caused by: java.lang.NullPointerException
	at com.binfoo.www.module.WeiXinUserModule.<init>(WeiXinUserModule.java:32)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
	at org.nutz.lang.born.EmptyArgsConstructorBorning.born(EmptyArgsConstructorBorning.java:16)
	... 45 more

shiro.ini配置
cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
cacheManager.cacheManagerConfigFile = classpath:ehcache.xml

代码

  Cache cache = cacheManager.getCache("bms");
    String access_token;

    Element ele = cache.get("key");

echcache.xml配置

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"
         monitoring="autodetect" dynamicConfig="true" name="bms">
    <!-- <diskStore path="java.io.tmpdir/shiro-ehcache"/> -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
    />
    <cache name="shiro-activeSessionCache"
           maxElementsInMemory="10000"
           overflowToDisk="true"
           eternal="true"
           timeToLiveSeconds="0"
           timeToIdleSeconds="0"
           diskPersistent="true"
           diskExpiryThreadIntervalSeconds="600"/>
</ehcache>

ehcache.js配置

var ioc = {
     cacheManager : {
          type : "net.sf.ehcache.CacheManager",
          factory : "net.sf.ehcache.CacheManager#getCacheManager",
          args : ["bms"] // 对应shiro.ini中指定的ehcache.xml中定义的name
     }
     /*
      // 如果不需要shiro初始化的Ehcache, 使用下面的方式配置
      cacheManager : {
      type : "net.sf.ehcache.CacheManager",
      factory : "net.sf.ehcache.CacheManager#create"
      }
      */
};
5 回复
WeiXinUserModule.<init>

执行构造方法之后,字段才能被注入的.

构造方法是对象建立的过程。对象还没建立完,字段还没机会注入呢

那我要在MainSetup里面进行注入?

知道@IocBean(create="init") 不?

这个指的是有用到的时候才进行构造?而不是初始化的时候就已经有了

ioc的属性/字段注入,是在对象生成之后进行的.

@IocBean(create="init")
public class XXX {
   public void init() {
       // 这里做各种初始化, 而不是在构造方法或字段/属性声明的地方做.
   }
}
添加回复
请先登陆
回到顶部