鍍金池/ 問(wèn)答/人工智能  Java/ JedisPool 注入spring 為 null

JedisPool 注入spring 為 null

最近在項(xiàng)目中(spring)集成 redis 時(shí),在去工廠實(shí)例時(shí),一直抱空指針 異常:
但是單元測(cè)試始終正常:
以下為 redis-config.xml 文件
 <context:property-placeholder location="classpath:properties/redis.properties" ignore-unresolvable="true"/>


    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.maxTotal}"/>
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxWaitMillis" value="${redis.maxWait}"/>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
    </bean>

    <!-- 單例模式 默認(rèn) -->
    <bean id="connectionFactory" class="redis.clients.jedis.JedisPool">
        <constructor-arg ref="jedisPoolConfig" name="poolConfig"/>
        <constructor-arg name="host" value="${redis.host}"/>
        <constructor-arg name="port" value="${redis.port}"/>
        <constructor-arg name="timeout" value="${redis.timeout}"/>
    </bean>

測(cè)試代碼:

@Test
    public void redisTest(){
        Jedis jedis=jedisConnectionFactory.getResource();
        jedis.set("123","312312");
        System.out.println(jedis.get("123"));
    }
    

測(cè)試運(yùn)行結(jié)果為:

312312

但是在 mybatis 中加入二級(jí)緩存抱錯(cuò)

  xxxMapper.xml
  ```xml
   <cache type="extra.cache.RedisCache"/>
   ```
  

二級(jí)緩存接口繼承處理...

@Component
public class RedisCache implements Cache {

    private static final Logger logger= LoggerFactory.getLogger(RedisCache.class);

    @Autowired
    private JedisPool jedisConnectionFactory;


    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    private final String id;


    public RedisCache(final String id){
        if(id==null){
            throw new IllegalArgumentException("Cache instances require an ID");
        }
        logger.debug("MybatisRedisCache:id=" + id);
        this.id=id;
    }

    /**
     *  獲取 id
     * @return
     */
    public String getId() {
        return this.id;
    }

    /**
     *  存儲(chǔ)數(shù)據(jù)到 redis 數(shù)據(jù)庫(kù)中
     * @param key
     * @param value
     */
    public void putObject(Object key, Object value) {

        logger.info("putObject :"+key+"= "+value);

        Jedis connection= jedisConnectionFactory.getResource();
        RedisSerializer<Object> serializer=new JdkSerializationRedisSerializer();
        connection.set(serializer.serialize(key),serializer.serialize(value));
        connection.close();

    }

    /**
     *  從 redis 中獲取 數(shù)據(jù)
     * @param key
     * @return
     */
    public Object getObject(Object key) {
        logger.info("jedisConnectionFactory : "+jedisConnectionFactory);

        Jedis connection= jedisConnectionFactory.getResource();
        RedisSerializer<Object> serializer=new JdkSerializationRedisSerializer();
        Object result=serializer.deserialize(connection.get(serializer.serialize(key)));
        connection.close();
        return result;

    }

報(bào)的錯(cuò)誤如下:打印日志說(shuō)明為 空對(duì)象(JedisPool)


  INFO  extra.cache.RedisCache - jedisConnectionFactory : null

Error querying database.  Cause: java.lang.NullPointerException
### Cause: java.lang.NullPointerException
  1. 初步懷疑為spring 加載順序的問(wèn)題,但是采用 SpringContextUtil.getBean("") ,依舊獲取不了 該實(shí)例

    public class SpringContextUtil implements ApplicationContextAware{...}
  2. 該項(xiàng)目采用多maven結(jié)構(gòu),同時(shí)子maven 打包時(shí),存在一系列問(wèn)題... 導(dǎo)致子模塊之間沒(méi)有通信
回答
編輯回答
執(zhí)念

文件沒(méi)有引入進(jìn)去,

不能直接在 web.xml 直接配置(解決方案是 import 引入)

具體原因,還待研究;

2017年11月18日 15:46