0 环境

  1. 系统环境:win10
  2. 编辑器:IDEA

1 前言->环境搭建

1-1 pom依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.8.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>org.javaboy</groupId>
  12. <artifactId>chapter09-cacheredis</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>chapter09-cacheredis</name>
  15. <description>Demo project for Spring Boot</description>
  16.  
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>
  20.  
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-cache</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-data-redis</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-security</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-web</artifactId>
  37. </dependency>
  38.  
  39. <dependency>
  40. <groupId>org.projectlombok</groupId>
  41. <artifactId>lombok</artifactId>
  42. <optional>true</optional>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter-test</artifactId>
  47. <scope>test</scope>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework.security</groupId>
  51. <artifactId>spring-security-test</artifactId>
  52. <scope>test</scope>
  53. </dependency>
  54. </dependencies>
  55.  
  56. <build>
  57. <plugins>
  58. <plugin>
  59. <groupId>org.springframework.boot</groupId>
  60. <artifactId>spring-boot-maven-plugin</artifactId>
  61. </plugin>
  62. </plugins>
  63. </build>
  64.  
  65. </project>

1-2 properties(redis的基本配置以及缓存名的配置)

  1. spring.redis.host=127.0.0.1
  2. spring.redis.password=123456
  3. spring.redis.port=6379
  4. spring.redis.database=0
  5.  
  6. spring.cache.cache-names=c1

1-3 Application启动项添加

1-4 自定义KeyGenerator

  1. @Component
  2. public class MyKeyGen implements KeyGenerator {
  3.  
  4. @Override
  5. public Object generate(Object o, Method method, Object... objects) {
  6. return method.getName()+":"+ Arrays.toString(objects);
  7. }

1-5 service层

  1. @Service
  2. //@CacheConfig(cacheNames = "c1")
  3. public class BookService {
  4. // key = "#methodName"
  5. // key = "#method.name"
  6. // key = "#caches[0]"
  7. // key = "#args[0]"
  8. // @Cacheable(cacheNames = "c1",key = "#caches[0]")
  9. @Cacheable(cacheNames = "c1",key = "'myKeyGen'")
  10. public Book getUserById(Integer id){
  11. System.out.println("book>>>>>>" + id);
  12. Book book = new Book();
  13. book.setId(id);
  14. return book;
  15. }
  16.  
  17. }

1-6 单元测试

  1. @Autowired
  2. BookService bookService;
  3.  
  4. @Test
  5. public void contextLoads() {
  6.  
  7. Book book = bookService.getUserById(1);
  8. Book book1 = bookService.getUserById(1);
  9. System.err.println("book --->>" + book);
  10. System.err.println("book1 --->>" + book1);
  11.  
  12. }

2 报错

  1. key = "''" (需要内嵌一下单引号 不然会报错)

cannot be found on object of type xx.CacheExpressionRootObject的更多相关文章

  1. PHP“Cannot use object of type stdClass as array” (php在调用json_decode从字符串对象生成json对象时的报错)

    php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误 错误:Cannot use object of type stdClass as arra ...

  2. PHP json_decode object时报错Cannot use object of type stdClass as array

    PHP json_decode object时报错Cannot use object of type stdClass as array php再调用json_decode从字符串对象生成json对象 ...

  3. MyBatis Generator报错:Cannot instantiate object of type

    [ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate ( ...

  4. spring boot 之 错误:SpelEvaluationException: EL1008E: Property or field 'timestamp' cannot be found on object of type 'java.util.HashMap'

    这个错误我也见过很多次了,今天终于理解了其出现的原因. 错误是这样的: 2017-11-23 18:05:39.504 ERROR 4092 --- [nio-8080-exec-3] o.a.c.c ...

  5. Unable to cast object of type 'System.Int32' to type 'System.Array'.

    x 入职了新公司.最近比较忙...一看博客...更新频率明显少了...罪过罪过... 新公司用ASP.NET MVC 遇上一个错误: Unable to cast object of type 'Sy ...

  6. iwebshop (: Cannot use object of type stdClass as array in)

    今天在PHP输出一个二维数组的时候,出现了“Fatal error: Cannot use object of type stdClass as array in……”. 这个二维数组是这样的: Ar ...

  7. celery 4.1下报kombu.exceptions.EncodeError: Object of type 'bytes' is not JSON serializable 处理方式

    #python代码如下 from celery import Celeryimport subprocess app = Celery('tasks', broker='redis://localho ...

  8. Object of type 'ListSerializer' is not JSON serializable “listserializer”类型的对象不可JSON序列化

    Object of type 'ListSerializer' is not JSON serializable “listserializer”类型的对象不可JSON序列化 一般原因为 序列化的对象 ...

  9. TypeError: Object of type 'int64' is not JSON serializable

    错误类型:TypeError: Object of type 'int64' is not JSON serializable 错误场景:对Numpy和Pandas结果进行json.dumps报错 错 ...

随机推荐

  1. java.io.tmpdir在哪里?

    查找所在目录的方式如下: System.out.println(System.getProperty(“java.io.tmpdir”)); System.getProperty(),还可以获取更多其 ...

  2. 吴裕雄--天生自然 JAVASCRIPT开发学习: DOM 事件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. tensorflow 损失计算--MSN

    1.tf.losses.mean_squared_error函数 tf.losses.mean_squared_error( labels, predictions, weights=1.0, sco ...

  4. GaussDB数据dump实现完全同步

    问题背景:搭建服务后端容灾集群,服务正常时容灾DB需要从业务DB完全同步数据,服务异常时,容灾DB停止抽取数据,自动从探针采集业务数据. 解决方案:常用的有两种思路,一是从服务后端定时每天拉取业务DB ...

  5. 将hello程序作为驱动程序编译进系统内核

    0x00开始 恩,可能是我比较愚钝,一个内核编译搞了一天,各种问题,各种bug,几度无奈,也是因为我突发奇想,并没有按照原来的那种操作,我直接把helloworld程序放到内核模块中编译成了一个驱动程 ...

  6. Django模型基础(三)——关系表的数据操作

    模型之间可以有三种表关系,即一对一,一对多和多对多.表关联之间的数据操作在Django中可以很方便的操作到.在模型中,表关联的字段类型是关联表的实例,而不是字段本身类型.关联字段在数据库中会在其后补上 ...

  7. Codeforces Round #568 (Div. 2)网卡&垫底记

    这场和div3差不多嘛(后来发现就是div3),就是网太卡10min交一发就不错了,简直自闭. A 签到. B 记录每一段的字母数,满足条件即:段数相同+字母相同+字母数下>=上. #inclu ...

  8. 1. react 基础 简介 及 环境搭建

    一.简介 由 Facebook 推出 2013 年 开源 的 函数式编程的 使用人数最多的 前端框架 拥有健全的文档与完善的社区 ( 官网 ) react 16 称为 React Fiber ( 底层 ...

  9. 题解 P1829 【[国家集训队]Crash的数字表格 / JZPTAB】

    题目 我的第一篇莫比乌斯反演题解 兴奋兴奋兴奋 贡献一个本人自己想的思路,你从未看到过的船新思路 [分析] 显然,题目要求求的是 \(\displaystyle Ans=\sum_{i=1}^n\su ...

  10. 一、早期(Early Stage)

    一.早期(Early Stage) 如果单纯从零基础开始,早期(Early Stage)应该是一到两个月(由于英语与中文差异比与其他语言大,中国同学至少两个月,但也不应过长.我们的经验是一般中国同学会 ...