springboot aop学习

依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.12</version></dependency>

annotation

package com.xy.boot.annotation;import java.lang.annotation.*;@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface XyAnnotation {String value() default "";}

 aspect

package com.xy.boot.aspect;import com.xy.boot.annotation.XyAnnotation;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Arrays;@Aspect
@Component
public class XyAspect {//  @Pointcut("execution(public * com.xy.boot.controller.*.*(..))")指定拦截com.xy.boot.controller包下所有类的所有方法//  @Pointcut("execution(public * com.xy.boot.*.*.*(..))")指定拦截com.xy.boot.*.*包下所有类的所有方法//  @Pointcut("execution(public * com.xy..*.*(..))")指定拦截com.xy包下所有包的类的所有方法@Pointcut("@annotation(com.xy.boot.annotation.XyAnnotation)")public void pointcut() {}@Around(value = "pointcut()")public Object log(ProceedingJoinPoint joinPoint) throws Throwable {Object result = joinPoint.proceed();Object target = joinPoint.getTarget();Object[] args = joinPoint.getArgs();//do something elseSystem.out.println("target: " + target);System.out.println("args: " + Arrays.toString(args));//获取当前登录人信息//Subject subject = SecurityUtils.getSubject();//SysUser user = (SysUser)subject.getPrincipal();//获取RequestAttributesRequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();//从获取RequestAttributes中获取HttpServletRequest的信息HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);String token = request.getHeader("token");String xy = request.getHeader("xy");System.out.println("token: " + token);System.out.println("xy: " + xy);System.out.println("xyxy: " + request.getParameter("xyxy"));MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method22 = signature.getMethod();XyAnnotation annotation = method22.getAnnotation(XyAnnotation.class);String s = annotation.value();//获取注解内的的值sSystem.out.println("拿到注解里的值:" + s);ProceedingJoinPoint jp = joinPoint;Class<?> targetCls = jp.getTarget().getClass();MethodSignature ms = (MethodSignature) jp.getSignature();Method method = targetCls.getDeclaredMethod(ms.getName(), ms.getParameterTypes());XyAnnotation xyAnnotation = method.getAnnotation(XyAnnotation.class);if (xyAnnotation != null) {System.out.println("拿到注解里的值:" + xyAnnotation.value());}System.out.println("执行结果:"+result);return result;}@AfterThrowing(value = "pointcut()", throwing = "t")public void afterThrowing(JoinPoint point, Throwable t) {System.out.println("异常通知" + t.getMessage());}}

controller

package com.xy.boot.controller;import cn.hutool.extra.spring.SpringUtil;
import com.xy.boot.annotation.XyAnnotation;
import com.xy.boot.aspect.XyAspect;
import com.xy.boot.controller.service.XyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/test")
public class TestAspectController {@Autowiredprivate XyService xyService;// http://127.0.0.1:8080/test/hello?name=lisi@RequestMapping("/hello")public String hello(@RequestParam(name = "name", defaultValue = "unknown user") String name) {return "Hello " + name;}// http://127.0.0.1:8080/test/aspect?xyxy=xy666@RequestMapping("/aspect")@ResponseBody@XyAnnotation("---")public String aspect(@RequestParam(name = "xyxy", defaultValue = "unknown user") String name) {XyAspect bean = SpringUtil.getBean(XyAspect.class);System.out.println("bean = " + bean);return "Hello " + name;}// http://127.0.0.1:8080/test/hi?name=lisi@GetMapping("/hi")public Object hi(@RequestParam(name = "name", defaultValue = "unknown user") String name) {return xyService.sayHello(name);}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/849064.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring-Cloud-Gateway-实现XSS、SQL注入拦截

XSS和SQL注入是Web应用中常见计算机安全漏洞&#xff0c;文章主要分享通过Spring Cloud Gateway 全局过滤器对XSS和SQL注入进行安全防范。 写这篇文章也是因为项目在经过安全组进行安全巡检时发现项目存储该漏洞后进行系统整改&#xff0c;本文的运行结果是经过安全组验证通过。…

玩物科技:引领物联网时代的创新先锋

在深圳这座充满活力和创新精神的城市&#xff0c;有一家年轻而充满潜力的公司正在悄然改变我们的日常生活。深圳市玩物科技有限公司自2017年成立以来&#xff0c;凭借其卓越的技术和创新理念&#xff0c;逐渐成为物联网时代的先锋力量。 玩物科技的愿景与使命 玩物科技的核心…

【vue3响应式原理】

# 源码结构 源码位置是在packages文件件内&#xff0c;实际上源码主要分为两部分&#xff0c;编译器和运行时环境 1. 编译器 compiler-core 核心编译逻辑compiler-dom 针对浏览器平台编译逻辑compiler-sfc 针对单文件组件编译逻辑compiler-ssr 针对服务端渲染编译逻辑 2. 运行时…

使用kafka tools工具连接带有用户名密码的kafka

使用kafka tools工具连接带有用户名密码的kafka 创建kafka连接&#xff0c;配置zookeeper 在Security选择Type类型为SASL Plaintext 在Advanced页面添加如下图红框框住的内容 在JAAS_Config加上如下配置 需要加的配置&#xff1a; org.apache.kafka.common.security.plain.Pla…

企业数字化转型的主要方面有哪些?

本人研究企业数字化转型10余年&#xff0c;为企业软件选型、数字化提供咨询服务&#xff01;目前重点研究低代码数字化转型玩法&#xff0c;力争为各行各业探索出一条最具性价比的数字化方式。 关于“企业数字化转型包括哪些方面”这个问题&#xff0c;咱先来看个例子哈~ 比如…

用负载绿原酸的纳米复合水凝胶调节巨噬细胞表型以加速伤口愈合

引用信息 文 章&#xff1a;Modulating macrophage phenotype for accelerated wound healing with chlorogenic acid-loaded nanocomposite hydrogel. 期 刊&#xff1a;Journal of Controlled Release&#xff08;影响因子&#xff1a;10.8&#xff09; 发表时间&a…

基于pytoch卷积神经网络水质图像分类实战

具体怎么学习pytorch&#xff0c;看b站刘二大人的视频。 完整代码&#xff1a; import numpy as np import os from PIL import Image import torch import torch.nn as nn import torch.optim as optim from torchvision import datasets, transforms from torch.utils.data…

resultType的类型错误

resultType的类型错误&#xff0c;不能是List而应该是对应的返回Bean对象的类型&#xff0c;VO 这里是引用 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: Error querying database. Cause: java.lang…

opencv进阶 ——(十二)基于三角剖分实现人脸对齐

三角剖分概念 三角剖分&#xff08;Triangulation&#xff09;是一种将多边形或曲面分解为一系列互不相交的三角形的技术&#xff0c;它是计算几何、计算机图形学、地理信息系统、工程和科学计算中的一个基本概念。通过三角剖分&#xff0c;复杂的形状可以被简化为基本的三角…

病理级Polymer酶标二抗IHC试剂盒上线!

免疫组织化学 Immunohistochemistry,lHC 是利用抗体与抗原特异性识别原理&#xff0c;对组织样本中的抗原进行定位/定性分析的实验技术。组织切片保留了样品的解剖学结构特征&#xff0c;从而可以高分辨率地显现蛋白在细胞&#xff0c;甚至细胞器中的定位。基于以上特性&…

生物相容性CY5.5-D-甘露糖细胞生物学研究

随着生物医学研究的深入发展&#xff0c;荧光标记技术在细胞生物学中的应用日益广泛。其中&#xff0c;CY5.5-D-甘露糖作为一种新型的荧光标记物&#xff0c;不仅继承了CY5.5荧光染料的光学性能&#xff0c;还结合了D-甘露糖的生物学特性&#xff0c;因此在细胞成像、药物研发等…

DBus 在Qt和C++中的使用Demo

一、DBus DBus&#xff08;D-Bus&#xff09;是一种跨进程通信机制&#xff0c;是一种消息总线系统。DBus提供了一种在应用程序之间进行通信和交互的方式&#xff0c;可以在不同的进程之间传递消息&#xff0c;并提供了一套API供开发者使用。 二、Qt中使用 功能&#xff1a;先获…

Apple - Image I/O Programming Guide

翻译自&#xff1a;Image I/O Programming Guide&#xff08;更新时间&#xff1a;2016-09-13 https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/ImageIOGuide/imageio_intro/ikpg_intro.html#//apple_ref/doc/uid/TP40005462 文章目录 …

orbslam2代码解读(1):数据预处理过程

写orbslam2代码解读文章的初衷 首先最近陆陆续续花了一两周时间学习视觉slam&#xff0c;因为之前主要是做激光slam&#xff0c;有一定基础所以学的也比较快&#xff0c;也是看完了视觉14讲的后端后直接看orbslam2的课&#xff0c;看的cvlife的课&#xff08;课里大部分是代码…

jenkins的简单使用

2.1.简介 Jenkins是一个开源软件项目&#xff0c;是基于Java开发的一种持续集成工具&#xff0c;用于监控持续重复的工作&#xff0c;旨在提供一个开放易用的软件平台&#xff0c;使软件的持续集成变成可能。 2.4.Jenkins安装 1.下载安装包jenkins.war&#xff1b; 2.在安装…

笔记 | 软件工程04:软件项目管理

1 软件项目及其特点 1.1 什么是项目 1.2 项目特点 1.3 影响项目成功的因素 1.4 什么是软件项目 针对软件这一特定产品和服务的项目努力开展“软件开发活动",&#xff08;理解&#xff1a;软件项目是一种活动&#xff09; 1.5 软件项目的特点 1.6 军用软件项目的特点 2 …

一、搭建 Vue3 Admin 项目:从无到有的精彩历程

在前端开发的领域中&#xff0c;Vue3 展现出了强大的魅力&#xff0c;而搭建一个功能丰富的 Vue3 Admin 项目更是充满挑战与乐趣。今天&#xff0c;我将和大家分享我搭建 Vue3 Admin 项目的详细过程&#xff0c;其中用到了一系列重要的依赖包。 首先 让我们开启这个旅程。在确…

怎么用电脑把图片转换二维码?图片在线生成二维码的步骤内容

现在很多人会通过二维码来存储物品的信息图片&#xff0c;其他人可以通过扫描二维码的方式来查看对应的图片内容&#xff0c;那么当我们需要将一批图片每个单独生成二维码&#xff0c;该如何操作能够快速将图片转换二维码呢&#xff1f; 今天&#xff0c;小编来分享给大家一个…

CNN卷积神经网络

一、概述 卷积神经网络&#xff08;CNN&#xff09;是深度学习领域的重要算法&#xff0c;特别适用于处理具有网格结构的数据&#xff0c;比如说图像和音频。它起源于二十世纪80至90年代&#xff0c;但真正得到快速发展和应用是在二十一世纪&#xff0c;随着深度学习理论的兴起…

【ai】phc:安装issac环境且fix libstdc++.so 版本报错

Pycharm远程连接服务器(2023-11-9) 大神分享了pycharm远程连接ubuntu工作站的方法。 https://github.com/ZhengyiLuo/PHC 给出的操作同样适用: 参考 Pycharm远程连接服务器(2023-11-9) :前提是一样的 PHC的要求:isaac 创建 conda activate isaac