首页 > 其他分享 >Springboot Cache @Cacheable 类内部调用时不生效,解决办法

Springboot Cache @Cacheable 类内部调用时不生效,解决办法

时间:2023-11-02 12:34:37浏览次数:28  
标签:Cacheable return Springboot Cache getBean springframework org import public

出现问题的原因:Spring cache的实现原理是基于AOP的动态代理实现的:即都在方法调用前后去获取方法的名称、参数、返回值,然后根据方法名称、参数生成缓存的key(自定义的key例外),进行缓存。this调用不是代理对象的调用, 所以aop失效,注解失效。

解决办法就是,我们获取当前Bean,由它来调用。

SpringContextUtil

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 *不依赖于Autowired而使用java代码快速获取到spring管理的bean
 */
@Component
@Lazy(false)
public class SpringContextUtil  implements ApplicationContextAware {
     private static ApplicationContext applicationContext;
     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
         SpringContextUtil .applicationContext = applicationContext;
     }
     public static ApplicationContext getApplicationContext() {
         return applicationContext;
     }
     public static Object getBean(String name) {
         return getApplicationContext().getBean(name);
     }
     public static <T> T getBean(Class<T> clazz) {
         return getApplicationContext().getBean(clazz);
     }
     public static <T> T getBean(String name, Class<T> clazz) {
         return getApplicationContext().getBean(name, clazz);
     }
}

 

Service 调用

package com.mark.pay.service;

import com.mark.pay.bean.User;
import com.mark.pay.common.spring.SpringContextUtil;
import com.mark.pay.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;

@Service
@CacheConfig(cacheNames = "user",cacheManager = "cacheManager")
public class TestService {

    @Autowired
    UserMapper userMapper;

    @Cacheable( key ="#id")
    public User query(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }


    public User testCacheQuery(Integer id){
        TestService service = SpringContextUtil.getBean(TestService.class);
        return service.query(id);
    }
}

 

标签:Cacheable,return,Springboot,Cache,getBean,springframework,org,import,public
From: https://www.cnblogs.com/roak/p/17805125.html

相关文章

  • springboot post请求的content-type
    content-type是http请求的响应头和请求头的字段。当作为响应头时,告诉客户端实际返回的内容的内容类型。作为请求头时(post或者put),客户端告诉服务器实际发送的数据类型。在前端开发过程中,通常需要跟后端工程师对接接口的数据格式,不同的数据类型对于服务器来说有不同的处理方式,因此......
  • in org.springframework.cache.annotation.ProxyCachingConfiguration required a be
    我的项目是springboot项目,在启动过程中报错如何下Parameter0ofmethodcacheAdvisorinorg.springframework.cache.annotation.ProxyCachingConfigurationrequiredabeanoftype'org.springframework.cache.interceptor.CacheOperationSource'thatcouldnotbefound......
  • springboot正常启动的时候,@Configuration的@Bean属于初始化就得加载的,当该springboot
      ......
  • SpringBoot
    1.SrpingBoot入门及原理Spring是如何简化Java开发的为了降低Java开发的复杂性,Spring采用了以下4种关键策略:基于POJO的轻量级和最小侵入性编程,所有东西都是bean;通过IOC,依赖注入(DI)和面向接口实现松耦合;基于切面(AOP)和惯例进行声明式编程;通过切面和模版减少样式代码,RedisTem......
  • influxdb报错:cache-max-memory-size exceeded
    转载请注明出处:influxdb报错日志: 该错误信息表示InfluxDB引擎超过了缓存最大内存大小。这意味着InfluxDB的缓存使用量超出了配置的限制。要解决此问题,可以采取以下步骤来定位和解决:检查配置文件:首先,请确保InfluxDB配置文件中没有设置错误。在配置文件......
  • gin框架curd 和java springboot crud 的比较及性能
    Gin框架与SpringBoot框架的CURD比较Ginvs.SpringBoot:简介Gin(Go语言):Gin是用于构建Web应用程序和API的轻量级、高性能框架,使用Go编程语言。它以简洁和高性能而闻名。SpringBoot(Java):SpringBoot是一个用于构建基于Java的Web应用程序的开源Java框架。它简化了使......
  • Error loading wikitext data raise NotImplementedError(f"Loading a dataset cached
    ErrorloadingwikitextdataraiseNotImplementedError(f"Loadingadatasetcachedina{type(self._fs).name}isnotsupported.")QAIwastryingtoloadthewikidataset,butigotthiserrortraindata=load_dataset('wikitext','......
  • Java后台微信点餐小程序2023年最新版笔记Springboot+Mysql+Freemarker+Bootstrap
    由于之前的Java后台微信点餐小程序有些知识点过时了,所以今天重新出一版,把里面过时的知识点更新下第一章,技术选型(重要)在开始学习之前,要记得安装jdk8和mysql8,后面的笔记里也会具体讲解怎么安装,但是jdk8和mysql8必须和石头哥保持一致。1,后台技术选型:JDK8(必须保持一致)Mysql8(必......
  • SpringBoot数据响应、分层解耦、三层架构
    响应数据@ResponseBody类型:方法注解、类注解位置:Controller方法、类上作用:将方法返回值直接响应,如果返回值类型是实体对象/集合,将会转换为json格式响应说明:@RestController=@Controller+@ResponseBody统一响应结果步骤:获取员工数据,返回统一响应结果,在页面渲染......
  • Jenkins+Docker 一键自动化部署 SpringBoot 项目
    Jenkins和Docker是现代软件开发中非常流行的工具,可以帮助我们自动化构建、测试和部署应用程序。SpringBoot是一种流行的Java框架,可以帮助开发人员快速开发Web应用程序。在本文中,我们将介绍如何使用Jenkins和Docker一键自动化部署SpringBoot应用程序。准备工作首先,你需要安装并配......