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;
    }
}
相关推荐
Forget the Dream26 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
jonyleek29 分钟前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明
java·大数据·低代码·数据分析·软件需求
计算机小白一个1 小时前
蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)
java·数据结构·算法·蓝桥杯
曼岛_1 小时前
[密码学实战]Java实现SM4加解密(ecb,cbc)及工具验证
java·密码学
Forget the Dream1 小时前
设计模式之代理模式
java·c++·设计模式·代理模式
帅的飞起来1 小时前
设计模式--spring中用到的设计模式
java·spring·设计模式
張葒兵3 小时前
记一次命令行启动springboot项目的问题 java -jar的问题
java·spring boot·jar
zr想努力3 小时前
Lua的table类型的增删改查操作
java·开发语言·lua
Forget the Dream4 小时前
设计模式之享元模式
java·c++·设计模式·享元模式
sanshizhang4 小时前
eclasticsearch文档搜索
java·elasticsearch