欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > springboot aop学习

springboot aop学习

2025/4/19 17:38:58 来源:https://blog.csdn.net/qq_36895457/article/details/139512780  浏览:    关键词: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);}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词