0 环境

系统环境:win10
编辑器:IDEA

1 前言->环境搭建

1-1 pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.javaboy</groupId>
<artifactId>chapter09-cacheredis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>chapter09-cacheredis</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

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

spring.redis.host=127.0.0.1
spring.redis.password=123456
spring.redis.port=6379
spring.redis.database=0 spring.cache.cache-names=c1

1-3 Application启动项添加

1-4 自定义KeyGenerator

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

1-5 service层

@Service
//@CacheConfig(cacheNames = "c1")
public class BookService {
// key = "#methodName"
// key = "#method.name"
// key = "#caches[0]"
// key = "#args[0]"
// @Cacheable(cacheNames = "c1",key = "#caches[0]")
@Cacheable(cacheNames = "c1",key = "'myKeyGen'")
public Book getUserById(Integer id){
System.out.println("book>>>>>>" + id);
Book book = new Book();
book.setId(id);
return book;
} }

1-6 单元测试

    @Autowired
BookService bookService; @Test
public void contextLoads() { Book book = bookService.getUserById(1);
Book book1 = bookService.getUserById(1);
System.err.println("book --->>" + book);
System.err.println("book1 --->>" + book1); }

2 报错

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. 最短路问题-- Dijkstra Choose the best route

    Choose the best route Problem Description One day , Kiki wants to visit one of her friends. As she i ...

  2. css 设置奇数行偶数行样式

    .list ul li:nth-child(even){ ...}    //li的偶数行样式 .list ul li:nth-child(odd){ ...}    //li的奇数行样式 .list ...

  3. Django框架(十一):模板介绍、模板语言、模板继承、HTML转义

    1. 模板介绍 1.1 模板的功能 产生html,控制页面上展示的内容.模板文件不仅仅是一个html文件. 模板文件包含两部分内容: 静态内容:css.js.html. 动态内容:用于动态去产生一些页 ...

  4. Java正则表达式基础知识整理

    指定为字符串的正则表达式必须首先被编译为此类的实例.然后,可将得到的模式用于创建 Matcher 对象,依照正则表达式,该对象可以与任意字符序列匹配.执行匹配所涉及的所有状态都驻留在匹配器中,所以多个 ...

  5. 01 语言基础+高级:1-9 网络编程_day11【网络编程】

    day11[网络编程] 主要内容 软件架构CS/BS 网络通信三要素 TCP通信 Socket套接字 ServerSocket 教学目标 能够辨别UDP和TCP协议特点 能够说出TCP协议下两个常用类 ...

  6. C++常用库函数 C函数库 cstdio

    常用的C/C++函数库, cstdio(stdio.h) 标准输入输出库.C Standard Input and Output Library 1. 实例 #include <cstdio&g ...

  7. JavaWeb中的高级知识总结

    知识结构图 文件下载 默认情况下,如果浏览器可以处理Content-Type响应头中指定数据类型,浏览器就会直接处理,比如显示出HTML页面(text/html),或者显示出照片(image/png) ...

  8. jacascript 滚动scroll

    滚动 scroll scrollHeight 表示元素的总高度,包括由于溢出而无法展示在网页的不可见部分: scrollWidth 表示元素的总宽度,包括由于溢出而无法展示在网页的不可见部分: 没有滚 ...

  9. python爬虫破解带有CryptoJS的aes加密的反爬机制

    发现问题 在一次偶然中,在爬取某个公开网站(非商业型网站)时,老方法,打开调试工具查看请求方式,请求拦截,是否是异步加载,不亦乐乎,当我以为这个网站非常简单的时候,发现二级网页的地址和源码不对应 Aj ...

  10. 吴裕雄--天生自然 PYTHON3开发学习:条件控制

    if condition_1: statement_block_1 elif condition_2: statement_block_2 else: statement_block_3 var1 = ...