Spring boot @Aspect 注解切面无效

2022-06-27 14:38:48

Spring boot @Aspect 注解切面无效

发现少引入了Jar包 也没有 报错

注解类

package com.example.demo.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interfaceLog{
    Stringtitle()default"";}

切面类

package com.example.demo.Aspect;import com.example.demo.annotation.Log;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.Signature;import org.aspectj.lang.annotation.*;import org.aspectj.lang.reflect.MethodSignature;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import java.lang.reflect.Method;@Aspect@ComponentpublicclassLogAspect{privatestaticfinal Logger log= LoggerFactory.getLogger(LogAspect.class);// 配置织入点@Pointcut("@annotation(com.example.demo.annotation.Log)")publicvoidlogPointCut(){}/**
     * 处理完请求后执行
     *
     * @param joinPoint 切点
     */@AfterReturning(pointcut="logPointCut()", returning="jsonResult")publicvoiddoAfterReturning(JoinPoint joinPoint, Object jsonResult){handleLog(joinPoint, null, jsonResult);}/**
     * 拦截异常操作
     *
     * @param joinPoint 切点
     * @param e         异常
     */@AfterThrowing(value="logPointCut()", throwing="e")publicvoiddoAfterThrowing(JoinPoint joinPoint, Exception e){handleLog(joinPoint, e, null);}protectedvoidhandleLog(final JoinPoint joinPoint,final Exception e, Object jsonResult){try{// 获得注解
            Signature signature= joinPoint.getSignature();
            MethodSignature methodSignature=(MethodSignature) signature;
            Method method= methodSignature.getMethod();
            Log controllerLog= method.getAnnotation(Log.class);if(controllerLog== null){return;}
            System.out.println("输出log参数:"+controllerLog.title());
            System.out.println("输出请求参数:"+joinPoint.getArgs());

            String operLog="hhhhhhh";}catch(Exception exp){}}/**
     * 判断是否需要过滤的对象。
     *
     * @param o 对象信息。
     * @return 如果是需要过滤的对象,则返回true;否则返回false。
     *//*public boolean isFilterObject(final Object o) {
        return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse;
    }*/}

TestController

package com.example.demo.controller;import com.example.demo.annotation.Log;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import javax.xml.ws.Response;/**
 * @author txl
 * @date 2020-01-14-22:50
 */@Controller@RequestMapping(value="test")publicclassTestController{@Log(title="测试方法")@RequestMapping(value="/test",method= RequestMethod.GET)@ResponseBodypublic Stringtest( String param){

       String aa="{name: '小明' }";
        System.out.println("输出参数:"+param);return aa;}}

pom文件

<dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId></dependency>
  • 作者:捞小鱼
  • 原文链接:https://blog.csdn.net/lieyanchengbao/article/details/104431967
    更新时间:2022-06-27 14:38:48