spring aop

1、引入maven

bash 复制代码
	<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aop</artifactId>
				<version>3.2.3.RELEASE</version>
			</dependency>
			<dependency>
				<groupId>org.aspectj</groupId>
				<artifactId>aspectjweaver</artifactId>
				<version>1.7.2</version>
			</dependency>

2、注解打开

bash 复制代码
    <context:annotation-config/>
    <context:component-scan base-package="com.test" />

3、xml支持app

bash 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"

4、

bash 复制代码
@Aspect
@Component
public class AopInterceptor {
    @Pointcut("execution(* com.test.add*(..))")
    public void add() {

    }

    @Pointcut("execution(* com.test.update*(..))")
    public void update() {

    }

    @Pointcut("@annotation(com.test.aop.AsyncSendToMq)")
    public void asyncSendToMqAnnotation() {

    }

    @Around(value = "add() || update() || asyncSendToMqAnnotation()")
    public Object responseJsonTemplateConvert(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
        } finally {
            Object[] args = joinPoint.getArgs();
            for (Object arg : args) {
                System.out.println(arg);
            }
            String[] paramNames = ((CodeSignature) joinPoint.getSignature()).getParameterNames();
            System.out.println(paramNames);

            MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            //参数名数组
            String[] parameters = methodSignature.getParameterNames();
            System.out.println(parameters);
            for (String parameter : parameters) {
                System.out.println(parameter);
            }
            Map<String, Object> params = getNameAndValue(joinPoint);
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                System.out.println("name: " + entry.getKey() + " value: " + entry.getValue());
            }

        }

        return proceed;
    }

    /**
     * 获取参数Map集合
     *
     * @param joinPoint
     * @return
     */
    Map<String, Object> getNameAndValue(ProceedingJoinPoint joinPoint) {
        Map<String, Object> param = new HashMap<>();
        Object[] paramValues = joinPoint.getArgs();
        String[] paramNames = ((CodeSignature) joinPoint.getSignature()).getParameterNames();
        for (int i = 0; i < paramNames.length; i++) {
            param.put(paramNames[i], paramValues[i]);
        }
        return param;
    }
}
相关推荐
daidaidaiyu2 小时前
一文学习 工作流开发 BPMN、 Flowable
java
SuniaWang3 小时前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
sheji34163 小时前
【开题答辩全过程】以 基于springboot的扶贫系统为例,包含答辩的问题和答案
java·spring boot·后端
m0_726965984 小时前
面面面,面面(1)
java·开发语言
代码栈上的思考4 小时前
消息队列:内存与磁盘数据中心设计与实现
后端·spring
xuhaoyu_cpp_java4 小时前
过滤器与监听器学习
java·经验分享·笔记·学习
程序员小假5 小时前
我们来说一下 b+ 树与 b 树的区别
java·后端
Meepo_haha5 小时前
Spring Boot 条件注解:@ConditionalOnProperty 完全解析
java·spring boot·后端
sheji34166 小时前
【开题答辩全过程】以 基于springboot的房屋租赁系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端