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;
    }
}
相关推荐
小灰灰要减肥36 分钟前
装饰者模式
java
张铁铁是个小胖子1 小时前
MyBatis学习
java·学习·mybatis
Yan.love2 小时前
开发场景中Java 集合的最佳选择
java·数据结构·链表
椰椰椰耶2 小时前
【文档搜索引擎】搜索模块的完整实现
java·搜索引擎
大G哥2 小时前
java提高正则处理效率
java·开发语言
智慧老师2 小时前
Spring基础分析13-Spring Security框架
java·后端·spring
lxyzcm2 小时前
C++23新特性解析:[[assume]]属性
java·c++·spring boot·c++23
V+zmm101343 小时前
基于微信小程序的乡村政务服务系统springboot+论文源码调试讲解
java·微信小程序·小程序·毕业设计·ssm
Oneforlove_twoforjob3 小时前
【Java基础面试题025】什么是Java的Integer缓存池?
java·开发语言·缓存
xmh-sxh-13143 小时前
常用的缓存技术都有哪些
java