【redis】5.spring boot项目中,直接在spring data jpa的Repository层使用redis +redis注解@Cacheable直接在Repository层使用,报错问题处理Null key returned for cache operation
spring boot整合redis:http://www.cnblogs.com/sxdcgaq8080/p/8028970.html
首先,明确一下问题的场景
之前在spring boot整合redis,关于redis的使用都是在repository层上再封装一层service层,在service层上使用的。
现在如果直接将redis的注解放在repository上使用,是个什么情况呢?
代码如下:
1.首先我有一个实体XxAdmin,主键为id
2.Xxadmin我写了一个AdminRepository
package com.agen.myagen.repository; import com.agen.myagen.entity.XxAdmin;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.JpaRepository; /**
* admin持久化层
*
* @author SXD
* @date 2017/12/26
*/
public interface AdminRepository extends JpaRepository<XxAdmin,Integer> { /**
* 查找机构信息
* 并缓存到redis,键为id 值为XxAdmin
* @param adminId
* @return
*/
@Cacheable(value="admins", key="#adminId")
@Override
XxAdmin findOne(Integer adminId);
}
3.我在controller直接调用
package com.agen.controller; import com.agen.myagen.entity.XxAdmin;
import com.agen.myagen.repository.AdminRepository;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; @Controller
public class MainController { @Resource
private AdminRepository adminRepository; @RequestMapping("index")
public String getOrder(String adminId){
Integer adminID = Integer.parseInt(adminId);
XxAdmin admin = adminRepository.findOne(adminID);
return null;
} }
【报错】
启动之后,报错如下:Null key returned for cache operation (maybe you are using named params on classes without debug info?)
java.lang.IllegalArgumentException: Null key returned for cache operation (maybe you are using named params on classes without debug info?) Builder[public abstract com.agen.myagen.entity.XxAdmin com.agen.myagen.repository.AdminRepository.findOne(java.lang.Integer)] caches=[admins] | key='#adminId' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
报这个错,原因就是redis认定这个key是Null的,没办法存入,所以报错了。
但是之前咱们上一篇就是这么用的呀,查看上一篇,可以发现,redis是使用在service层,是在repository的再封装层使用的,那redis就不能直接使用在repository层了么?
【这里因为项目原因,不赘述为什么不封装一层service层使用,而是直接在repository层使用】【最后具体为什么key在这里会认定为null也没有找到原因,若知情,恳请告知!!!谢谢了】
【解决方法】
翻来覆去之后,发现redis的@Cacheable注解放在repository方法上,key会被认定为null,导致存不进redis缓存。
所以,换一种思路来解决这个问题,就是提前设定一种key的生成策略,即在RedisConfig类中指定一种KeyGenerator。【这一步骤的解释,可以查看http://www.cnblogs.com/sxdcgaq8080/p/8028970.html】
具体的解决方法如下:
在RedisConfig中,设定仅取第一个参数作为key的key生成策略
/**
* 生成key的策略【自定义第三种】
* 使用范围:仅适用于选取第一个参数做键的情况
* 由于reposotory上不能直接使用spel表达式作key,故而采用key的生成策略的方式来替换
*
* 使用时在注解@Cacheable(value = "admins",keyGenerator = "firstParamKeyGenerator")中指定
* @return
*/
@Bean(name = "firstParamKeyGenerator")
public KeyGenerator firstParamKeyGenerator(){
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(params[0].toString());
return sb.toString();
}
};
}
然后在AdminRepository中更换@Cacheable中的属性
public interface AdminRepository extends JpaRepository<XxAdmin,Integer> {
/**
* 查找机构信息
* 并缓存到redis,键为id 值为XxAdmin
* @param adminId
* @return
*/
@Cacheable(value="admins", keyGenerator = "firstParamKeyGenerator")
@Override
XxAdmin findOne(Integer adminId);
}
然后再去访问一次,
控制台成功从数据库查询了Xxadmin对象

查看redis中,已经将本对象存入缓存

再次访问,发现并未执行SQL语句,直接从缓存中取出本对象。
OK,直接在spring boot的repository层使用redis注解成功。
==============================================================================================================================================
最后,可以将AdminRepository类上使用
@CacheConfig(cacheNames = "admins")
指定本类中所有的方法,操作的缓存都是名为admins的缓存!!
package com.agen.myagen.repository; import com.agen.myagen.entity.XxAdmin;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.JpaRepository; /**
* admin持久化层
*
* @author SXD
* @date 2017/12/26
*/
@CacheConfig(cacheNames = "admins")
public interface AdminRepository extends JpaRepository<XxAdmin,Integer> { /**
* 查找机构信息
* 并缓存到redis,键为id 值为XxAdmin
* @param adminId
* @return
*/
@Cacheable(keyGenerator = "firstParamKeyGenerator")
@Override
XxAdmin findOne(Integer adminId);
}
具体可以参考:http://www.cnblogs.com/sxdcgaq8080/p/7228163.html查看这几个注解的使用场景
==============================================================================================================================================
本系列的源代码,可以从GitHub上获取查看:https://github.com/AngelSXD/myagenorderdiscount,类名及方法名都是对应的。所以想查看这部分使用的,可以直接在项目中查看即可!!
【redis】5.spring boot项目中,直接在spring data jpa的Repository层使用redis +redis注解@Cacheable直接在Repository层使用,报错问题处理Null key returned for cache operation的更多相关文章
- Spring Boot项目中使用Mockito
本文首发于个人网站:Spring Boot项目中使用Mockito Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试 ...
- 在Spring Boot项目中使用Spock测试框架
本文首发于个人网站:在Spring Boot项目中使用Spock测试框架 Spock框架是基于Groovy语言的测试框架,Groovy与Java具备良好的互操作性,因此可以在Spring Boot项目 ...
- Spring Boot项目中如何定制拦截器
本文首发于个人网站:Spring Boot项目中如何定制拦截器 Servlet 过滤器属于Servlet API,和Spring关系不大.除了使用过滤器包装web请求,Spring MVC还提供Han ...
- Spring Boot项目中如何定制PropertyEditors
本文首发于个人网站:Spring Boot项目中如何定制PropertyEditors 在Spring Boot: 定制HTTP消息转换器一文中我们学习了如何配置消息转换器用于HTTP请求和响应数据, ...
- Spring Boot项目中如何定制servlet-filters
本文首发于个人网站:Spring Boot项目中如何定制servlet-filters 在实际的web应用程序中,经常需要在请求(request)外面增加包装用于:记录调用日志.排除有XSS威胁的字符 ...
- 你真的理解 Spring Boot 项目中的 parent 吗?
前面和大伙聊了 Spring Boot 项目的三种创建方式,这三种创建方式,无论是哪一种,创建成功后,pom.xml 坐标文件中都有如下一段引用: <parent> <groupId ...
- Spring Boot项目中使用Swagger2
Swagger2是一款restful接口文档在线生成和在线接口调试工具,Swagger2在Swagger1.x版本的基础上做了些改进,下面是在一个Spring Boot项目中引入Swagger2的简要 ...
- 在Spring Boot项目中使用Spock框架
转载:https://www.jianshu.com/p/f1e354d382cd Spock框架是基于Groovy语言的测试框架,Groovy与Java具备良好的互操作性,因此可以在Spring B ...
- Spring Boot2 系列教程(三)理解 Spring Boot 项目中的 parent
前面和大伙聊了 Spring Boot 项目的三种创建方式,这三种创建方式,无论是哪一种,创建成功后,pom.xml 坐标文件中都有如下一段引用: <parent> <groupId ...
随机推荐
- MVC&JQuery如何根据List动态生成表格
背景:在编码中,常会遇到根据Ajax的结果动态生成Table的情况,本篇进行简要的说明.这已经是我第4.5篇和Ajax有关的随笔了,互相之间有很多交叠的地方,可自行参考. 后台代码如下: public ...
- Linux系统监视工具
转自 http://bbs.51cto.com/thread-971896-1.html # 1: top – 查看活动进程的命令TOP工具能够实时显示系统中各个进程的资源占用状况.默认情况 ...
- PHP-redis命令之 列表(lists)
三.列表(lists) 1.lpush:将所有指定的值插入到存于 key 的列表的头部.如果 key 不存在,那么在进行 push 操作前会创建一个空列表. 如果 key 对应的值不是一个 list ...
- while else语句
#else 用于检测循环中间是否有被打断count = 0while count <=5: print('loop',count) count +=1else: print('程序正常执行完毕, ...
- Python之code对象与pyc文件(二)
上一节:Python之code对象与pyc文件(一) 创建pyc文件的具体过程 前面我们提到,Python在通过import或from xxx import xxx时会对module进行动态加载,如果 ...
- 光学字符识别OCR-6 光学识别
经过前面的文字定位和文本切割,我们已经能够找出图像中单个文字的区域,接下来可以建立相应的模型对单字进行识别. 模型选择 在模型方面,我们选择了深度学习中的卷积神经网络模型,通过多层卷积 ...
- PHP 函数 ignore_user_abort()
ignore_user_abort 设置与客户机断开是否会终止脚本的执行. 本函数返回 user-abort 设置的之前的值(一个布尔值). int ignore_user_abort ([ st ...
- css各属性浏览器的兼容情况
- 让Android软键盘默认进入英文键盘
今天在做一个功能的 时候,需要输入法软键盘弹出后,需要进入英文输入界面. 可以通过设置EditText的输入类型为EMAIL来实现. //将输入法切换到英文 edit.setInput ...
- iOS------手势操作(nib文件、纯代码)
总共有六种手势识别:轻击手势(TapGestureRecognizer),轻扫手势 (SwipeGestureRecognizer), 长按手势(LongPressGestureRecognizer) ...