天天看點

在springmvc中配置jedis(轉)

主要學習https://github.com/thinkgem/jeesite。一下代碼均參考于此并稍作修改。

首先,需要添加jedis:

然後,springmvc完成基本配置。添加jedispool的bean即可。在spring容器中添加applicationContext-jedis.xml:

在applicationContext-jedis.xml中添加:

在springmvc中配置jedis(轉)
在springmvc中配置jedis(轉)

注解:參考的源碼中的jedisPool配置隻有三個參數:config,host,port。我複制後的結果總是getResource失敗,因為我的redis添加了auth,是以猜測是不是沒通過auth的原因。于是打開JedisPool的源碼:

在springmvc中配置jedis(轉)
在springmvc中配置jedis(轉)
在springmvc中配置jedis(轉)
在springmvc中配置jedis(轉)

View Code

看到有password的參數配置,如果沒有配置的話預設為null。到這一步我便沒有往下深入看了,因為我連接配接的redis中有auth,原諒我的不求甚解。于是,我接着配置timeout和auth。timeout直接還是源碼的預設值。後面的代碼測試通過。在這裡我了解到spring的bean注入的幾個參數含義:比如property表示屬性注入,constructor表示構造函數的參數注入。

上面設定好了JedisPool,這裡就要擷取jedis。然後就可以利用jedis進行操作了。

在springmvc中配置jedis(轉)
在springmvc中配置jedis(轉)
在springmvc中配置jedis(轉)
在springmvc中配置jedis(轉)

我們平時用到的list基本都是ObjectList,即list的元素為object而不是String。這樣就需要特定方法來緩存了。

在springmvc中配置jedis(轉)
在springmvc中配置jedis(轉)
在springmvc中配置jedis(轉)
在springmvc中配置jedis(轉)

So do it,and change it,no regret!

http://www.cnblogs.com/woshimrf/p/5211253.html