首页 > 编程语言 >java面向切面编程---AOP之环绕通知

java面向切面编程---AOP之环绕通知

时间:2023-08-10 15:34:39浏览次数:42  
标签:java AOP bigscreen --- org import com annotation redisTemplate

package com.xlkh.bigscreen.common.aspect;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xlkh.bigscreen.common.utils.RedisDeviceUtil;
import com.xlkh.bigscreen.service.bigscreen.BigscreenRedisService;
import com.xlkh.bigscreen.service.census.BigscreenCensusSqlService;
import lombok.SneakyThrows;
import net.sf.json.JSONArray;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Aspect
@Component
public class MyAspect {


    @Autowired
    @Resource(name = "redisTemplate3")
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private RedisDeviceUtil redisDeviceUtil;
    @Value("${spring.redis3.database:3}")
    private int database;

    String params="";

    @Autowired
    private BigscreenCensusSqlService bigscreenCensusSqlService;

    @Autowired
    private BigscreenRedisService bigscreenRedisService;

    //这里是查询缓存的  所以应该放入缓存的操作
    public Logger logger=LoggerFactory.getLogger(MyAspect.class);

    //定义切面
    @Pointcut(value = "execution(* com.xlkh.bigscreen.controller.census.BigscreenCensusCommonController.getCensusBy*(..))")
    public void myPointcut(){

    }


    //定义环绕通知   缓存操作
    @SneakyThrows
    @Around("myPointcut()")
    public Object cacheOperation(ProceedingJoinPoint proceedingJoinPoint){

        //获取类名
        String className=proceedingJoinPoint.getTarget().toString();
        //获取方法名
        String methodName=proceedingJoinPoint.getSignature().getName();
        //获取参数
        Object[] array=proceedingJoinPoint.getArgs();
        ObjectMapper mapper=new ObjectMapper();
        logger.info("方法执行之前--"+className+":"+methodName+"传递的参数--"+mapper.writeValueAsString(array));
        String key="";
        if (array.length>0){
            for (int i = 0; i <array.length ; i++) {
                if (null!=array[i]){
                    key=key.concat("_"+(String) array[i]);
                }
            }
        }
        int i = key.indexOf("_");
        System.err.println("方法----"+methodName);
        String redisKey="CACHE:"+key.substring(i+1,key.length());
        //通过key去查询Redis的缓存
        Object object = redisTemplate.opsForValue().get(redisKey);
        if (null!=object){
            logger.info("查询的是Redis的数据,查询的key是:"+redisKey);
            JSONArray jsonArray = JSONArray.fromObject(object);
            return jsonArray;
        }
        //Redis未查询到结果--查询相应的数据源
        //环绕   控制整个目标函数去执行
        List<Object> proceed = (List<Object>) proceedingJoinPoint.proceed();

        if (proceed.size()==0){
            redisTemplate.opsForValue().set(redisKey,"[]");
            //10秒过期
            redisTemplate.expire(redisKey,10, TimeUnit.SECONDS);
        }else {
            redisTemplate.opsForValue().set(redisKey, JSON.toJSONString(proceed));
//                //2天过期
//                redisTemplate.expire(redisKey,2, TimeUnit.DAYS);
            //20秒过期
            redisTemplate.expire(redisKey,20, TimeUnit.SECONDS);
        }

        return proceed;

    }
}

  接口的路径

使用AOP的作用其实就是在访问方法的时候,根据类名方法名以及传入的参数组装成redis的key,然后去redis查询数据

 

标签:java,AOP,bigscreen,---,org,import,com,annotation,redisTemplate
From: https://www.cnblogs.com/dabu/p/17620462.html

相关文章

  • F1C100s支持从Nand启动了,顺便说下如何向 U-Boot SPL 添加一个Image Loader
    让F1C100s从SPINand启动Tableofcontents名词解释写在前面启动流程实现[内核][文件系统][打包烧录]优化思路更多参考资料名词解释名词解释SPL第二阶段程序加载器BootROM固化在芯片内部的程序,用来识别并加载固件写在前面本次实验平台基于......
  • 第五期(2022-2023)传统行业云原生技术落地调研报告——央国企篇
     随着国务院国资委印发《关于加快推进国有企业数字化转型工作的通知》,开启了国有企业数字化转型的新篇章。大型央、国企纷纷顺应趋势,加速云化布局,将数字化转型工作定位为“十四五”时期重点任务。同时,越来越多的企业通过云原生技术推动IT变革,驱动业务创新发展,促进企业自身以及......
  • nth-of-type 和 nth-chid的区别
    例子:.box:nth-of-type(even){}  .box:nth-chid(even){}<body><divclass="box"></div><divclass="item"></div><divclass="item"></div><divclass="box">......
  • apache/hop-web 2.5安装和简单入门
    一、使用Docker安装部署1、拉取镜像推荐使用下面的web版本dockerpullapache/hop:latestdockerpullapache/hop-web:latest2、部署a、简单部署(不使用用户名密码)dockerrun-p8080:8080apache/hop-web:latestb、使用用户名密码和相关数据库配置的部署docker文件......
  • npm uninstall -g @vue/cli 命令卸载脚手架失败
     解决方法如下:1、执行:npmconfigls-l命令;在展示信息的userconfig键值中找到.npmrc文件的路径;将.npmrc文件删除。查询结果如下: 2、执行:wherevue命令;删除查询出的两个文件。文件如下图: 3、执行:vue-V命令。发现vue命令已经不可用,说明已经删除成功。 ......
  • 精彩回顾 | D-Day深圳 & 上海站:高频策略研发再提速
    上周末,DolphinDB分别在上海及深圳成功举办了两场D-Day分享会,来自国内头部券商、公募基金以及多家私募机构的数十位核心策略研发、数据分析专家们分享了DolphinDB在量化交易各个环节的使用经验,并基于与同类技术栈的优劣势对比,交流了在日常投研交易过程中遇到的瓶颈及解决方案。......
  • Linux --- 修改控制台字体consolefont
    Linux---修改控制台字体consolefont用户层面对于debianlike的系统,可到如下路径查找consolefont$cd/usr/share/consolefonts/$lsArabic-Fixed15.psf.gzCyrSlav-VGA32x16.psf.gzFullGreek-VGA8.psf.gzLat15-VGA16.psf.gz......
  • 嵌入式 Linux ------ UBIFS 文件系统的使用
    LinuxUBIFS文件系统的使用平台环境如下设施版本CPUAllwinnerF1C100slinux6.4.0-rc4ubootv2023.07-rc4buildrootv2023.02闪存WinbondSPI-Nand128MBW25N01G从Buildroot生成UBIFS手动创建UBIFS参考如下内容3.制作ubifsUbifs的制作......
  • 《剑指Offer》-46-把数字翻译成字符串
    读题数字0~25分别对应了a~z一共26个字母现在给一个数字,比如12258,问可能对应多少种不同的翻译?比如:1,2,2,5,812,2,5,812,25,81,22,5,81,2,25,8一共5种思路使用动态规划的三要素:数组元素定义数组初始化状态转移方程1225有几种可能的翻译?1,2,2,51,22,51,2,2512,2,512,25也......
  • 【2023-08-09】仁者爱人
    20:00说别人错很容易,但重要的是自己怎么做才是对的。                                                 ——汪成为今天想东西时,忽然想到了连叔说过的一个本质现象:“市......