cannot be found on object of type xx.CacheExpressionRootObject

0 环境

系统环境:win10
编辑器:IDEA

1 前言->环境搭建

1-1 pom依赖



         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.8.RELEASE
         
    
    org.javaboy
    chapter09-cacheredis
    0.0.1-SNAPSHOT
    chapter09-cacheredis
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-cache
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-security
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.security
            spring-security-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

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 = "''" (需要内嵌一下单引号 不然会报错)

 

转载于:https://www.cnblogs.com/my-ordinary/p/11564114.html

你可能感兴趣的:(cannot be found on object of type xx.CacheExpressionRootObject)