【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 ...
随机推荐
- poj 3107 Godfather(树的重心)
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7885 Accepted: 2786 Descrip ...
- UVa 1309 DLX Sudoku
16×16的数独. 看白书学的DLX,有些细节还有待消化,贴个模板先. #include <cstdio> #include <cstring> #include <al ...
- jnative 使用
下载地址: JNative_1.4RC2_src.zip : http://jaist.dl.sourceforge.net/sourceforge/jnative/JNative_1.4RC2_sr ...
- Jquery+Ajax+asp.net+sqlserver-编写的通用邮件管理(源码)
开始 邮件管理通常用在各个内部系统中,为了方便快捷的使用现有的代码开发一个邮件管理系统而诞生的. 准备条件 这是我的设计表结构,大家一看就懂了 --邮件接收表CREATE TABLE [dbo]. ...
- creat-react-app/dva静态项目,用nginx部署在次级域名路径(如a.com/sub/)需要注意的几点
因为要把dist文件夹部署在一个域名的次级目录,没想到和运维同学一起折腾了一下午.. 放在这里备忘,也给后来的同学一些可查的中文资料: 1,dva/cra给你的模板index.html是在public ...
- [错误解决]Ubuntu中使用dpkg安装deb文件提示依赖关系问题,仍未被配置
使用dpkg进行软件安装时,提示:dpkg:处理软件包XXX时出错:依赖关系问题,仍未被配置 使用如下命令,sudo apt-get install -f 等分析完之后,重新使用dpkg –i XXX ...
- [错误处理]Vim卡死,无法输入是怎么回事?是不是按了Ctrl+S
在linux下使用终端的时候常常不经意的就僵死了,只能够重启来解决这个问题,后来发现常常是因为按了Ctrl+s. 经过查询Ctrl + s在终端下的含义是暂停使用该终端的用途,如果需要启用终端,需要按 ...
- PTA 10-排序6 Sort with Swap(0, i) (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/678 5-16 Sort with Swap(0, i) (25分) Given a ...
- LibreOJ2241 - 「CQOI2014」排序机械臂
Portal Description 给出一个\(n(n\leq10^5)\)个数的序列\(\{a_n\}\),对该序列进行\(n\)次操作.若在第\(i\)次操作前第\(i\)小的数在\(p_i\) ...
- Server-Side Rendering(服务端渲染)的优点与缺点
优点 1. SEO 客户端渲染,页面中只有初始的几个html容器,js生成内容填充到容器中,爬虫只能识别到初始的html容器,js生成的内容一般不会被识别,而服务端渲染直接给出html,爬虫可以识别到 ...