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;
    }
}
相关推荐
Anastasiozzzz7 分钟前
Java Lambda 揭秘:从匿名内部类到底层原理的深度解析
java·开发语言
骇客野人9 分钟前
通过脚本推送Docker镜像
java·docker·容器
铁蛋AI编程实战26 分钟前
通义千问 3.5 Turbo GGUF 量化版本地部署教程:4G 显存即可运行,数据永不泄露
java·人工智能·python
晚霞的不甘37 分钟前
CANN 编译器深度解析:UB、L1 与 Global Memory 的协同调度机制
java·后端·spring·架构·音视频
SunnyDays101138 分钟前
使用 Java 冻结 Excel 行和列:完整指南
java·冻结excel行和列
摇滚侠1 小时前
在 SpringBoot 项目中,开发工具使用 IDEA,.idea 目录下的文件需要提交吗
java·spring boot·intellij-idea
云姜.1 小时前
java多态
java·开发语言·c++
李堇1 小时前
android滚动列表VerticalRollingTextView
android·java
泉-java1 小时前
第56条:为所有导出的API元素编写文档注释 《Effective Java》
java·开发语言
寄存器漫游者2 小时前
Linux 软件编程 - IO 编程
linux·运维·spring