在使用spring-data-redis時使用junit測試報錯:
java.lang.IllegalArgumentException: template not initialized; call afterPropertiesSet() before using
通過跟蹤代碼發現是這一句出了問題:
Assert.isTrue(this.initialized, "template not initialized; call afterPropertiesSet() before using it");
而initialized是在public void afterPropertiesSet() 中被賦值為true的。
解決方式:
不能直接在代碼中用new來直接實例化,必須得用SpingBean的實例化方式來!
