千家信息网

aop切面、注解和参数怎么获取

发表于:2025-11-20 作者:千家信息网编辑
千家信息网最后更新 2025年11月20日,本篇内容介绍了"aop切面、注解和参数怎么获取"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!在工作中
千家信息网最后更新 2025年11月20日aop切面、注解和参数怎么获取

本篇内容介绍了"aop切面、注解和参数怎么获取"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

在工作中会经常使用aop,这里将aop使用基本方法,获取在切点中使用的获取参数、注解做一个样例。

定义需要切面的注解

@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface AnnDemo {    String value();    boolean isAop() default true;}

在需要进行切面的方法标注注解

@RestController@RequestMapping("/order")public class OrderController {    @Autowired    private OrderService orderService;    @RequestMapping("/all")    @AnnDemo(value = "all",isAop = false)    public List findAll() {        List list = orderService.getOrderList();        return list;    }    @RequestMapping("/page")    @AnnDemo(value = "page")    public List findPage(@RequestParam("username") String username) {        List listPage = orderService.getOrdersListPage();        return listPage;    }}

定义切面

在切面中获取切点注解,方法,参数的获取

@Aspect@Componentpublic class AspectDemo {    @Pointcut(value = "execution(* com.yin.freemakeradd.controller..*(..))")    public void excetionMethod() {}    @Pointcut(value = "execution(* com.yin.freemakeradd.controller..*(..)) && @annotation(AnnDemo)")    public void excetionNote() { }    @Before("excetionMethod()")    public void testBefore(JoinPoint joinPoint) {        System.out.println("----------------------------前置通知---");        Object[] args = joinPoint.getArgs();        for (Object arg : args) {            System.out.println(arg);        }    }    @Around(value = "execution(* com.yin.freemakeradd.controller..*(..)) && @annotation(AnnDemo)")    public Object  testBeforeNote(ProceedingJoinPoint joinPoint) throws Throwable {        //用的最多通知的签名        Signature signature = joinPoint.getSignature();        MethodSignature msg=(MethodSignature) signature;        Object target = joinPoint.getTarget();        //获取注解标注的方法        Method method = target.getClass().getMethod(msg.getName(), msg.getParameterTypes());        //通过方法获取注解        AnnDemo annotation = method.getAnnotation(AnnDemo.class);        Object proceed;        //获取参数        Object[] args = joinPoint.getArgs();        System.out.println(annotation.value());        System.out.println(annotation.isAop());        for (Object arg : args) {            System.out.println(arg);        }        if (Objects.isNull(annotation) || !annotation.isAop()) {            System.out.println("无需处理");            proceed = joinPoint.proceed();        }else {            System.out.println("进入aop判断");            proceed = joinPoint.proceed();            if(proceed instanceof List){                List proceedLst = (List) proceed;                if(!CollectionUtils.isEmpty(proceedLst)){                    TbOrder tbOrder = new TbOrder();                    tbOrder.setPaymentType("fffffffffffffffffff");                    ArrayList tbOrderLst = new ArrayList<>();                    tbOrderLst.add(tbOrder);                    return tbOrderLst;                }            }            System.out.println(proceed);        }        return proceed;    }}

aop中获取自定义注解的属性值

自定义注解

@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface SystemLog {     public String description() default "";}

用在方法上

@ResponseBody@ValidRequestBody@RequestMapping("/login")@SystemLog(description="登录")public GlobalResponse login(@RequestBody @Valid User user, BindingResult bindingResult){    ......}

获取注解的属性值

@Around("@annotation(com.xxx.xxx.xxx.SystemLog)")public Object around(ProceedingJoinPoint joinPoint) throws Throwable{    SystemLog systemLog = ((MethodSignature)joinPoint.getSignature()).getMethod().getAnnotation(SystemLog.class);        ......}

"aop切面、注解和参数怎么获取"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

注解 方法 参数 切面 内容 切点 属性 更多 知识 面的 实用 一个样 学有所成 接下来 困境 实际 情况 文章 案例 编带 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 软件开发部经理求职简历 软件开发员能力要求 山西奋钧网络技术有限公司 网络经常提示无法连接服务器 网络安全法表决通过时间 平谷区软件开发来电咨询 北京多智互联网科技 学生宿舍数据库 代码 国家网络安全宣传周知识竞赛 我的世界服务器账号达到限制 计算机网络技术自顶向下方法 浙江直销软件开发商 软件开发人员月工资 现代化网络安全近期价格 嘉定区大型网络技术厂家价格 淘宝服务器无限转圈 网络安全工程师必须加班吗 电脑云顶之弈无法连接服务器 关于网络安全的英语毕业论文 高中数据库管理技术教案 医院数据库涉及隐私吗 电脑保存文件不需要数据库 你对网络安全有什么看法作文 北京华悦网络技术 县网络安全周工作总结 浪潮服务器主板启动图标怎么换 网络安全会议参加常委 无线传感器网络技术及应用 网络安全中电 安卓课表软件开发
0