栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot项目切面编程之@Aspect

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot项目切面编程之@Aspect

场景:
aop切面编程想必大家都不陌生了,aspect可以很方便开发人员对请求指定拦截层,一般是根据条件切入到controller控制层,做一些鉴权、分析注解、获取类名方法名参数、记录操作日志等

引入 aop 依赖包:


    org.springframework.boot
    spring-boot-starter-aop

切入流程:
1、创建组件类并标记注解@Aspect
2、定义切入点标记注解@Pointcut
3、使用切入点切入标记注解@Before或者@After

代码案例:

package com.hkl.modules.aspect;

import com.hkl.annotation.FieldConvert;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;


@Component
@Aspect
@Slf4j
public class AopConfigure {


    
    @Pointcut("execution( * com.hkl.modules.controller..*(..))")
    public void getMethods() {

    }
    
    @Pointcut("@annotation(com.hkl.annotation.FieldConvert)")
    public void withAnnotationMethods() {
    }


    
    @Before(value = "getMethods() && withAnnotationMethods()")
    public void doBefore(JoinPoint joinPoint){
        //获取Servlet容器
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //获取request请求
        HttpServletRequest request = attributes.getRequest();
        //执行方法对象
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        //判断切入的方法是否标记xxx注解
        //boolean flag = method.isAnnotationPresent(xxx.class);
        //CollUtil.toList(role).contains(userType);

        
        //1、鉴权、解析request请求对象中设置的属性
        //2、反射解析注解、记录操作日志等
        

        log.info("测试切入{}成功,方法名:"+method.getName(), "@Before");
    }


    
    @After(value = "getMethods() && withAnnotationMethods()")
    public void doAfter(JoinPoint joinPoint) {
        //业务操作同 @Before 方式

        MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        log.info("测试切入{}成功,是否包含注解:"+method.isAnnotationPresent(FieldConvert.class), "@After");
        log.info("注解中的属性值:"+method.getAnnotation(FieldConvert.class).codeType());
    }


}

测试代码:

@FieldConvert(codeType = "aa")
@ApiOperation(value = "测试切面用")
@GetMapping(value = "/getTestAop")
CommonResult getTestAop(){
    return success("响应成功!");
}

测试结果成功切入: 

总结和注意:
1、定义切入点,支持指定包按路径切入,也支持指定是否标记某个注解切入
2、aop默认无法切入 private 修饰的方法,切入点表达式定义的修饰符要和被切入的方法修饰符一致,否则无法切入
3、如下切入点规则,可以切入 public 修饰或者省略修饰符的方法,推荐此方式

@Pointcut("execution( * com.hkl.modules.controller..*(..))")

4、如果配置成 @Pointcut("execution(public * com.hkl.modules.controller..*(..))") 只能切入 public 修饰的方法

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/605589.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号