posts - 609,  comments - 13,  views - 64万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

引入pom

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
    <version>2.6.1</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.7</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.5.4</version>
</dependency>

  新建AOP类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.geebox.wmstest.config;
 
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
 
@Aspect
@Component
public class WebLogAcpect {
    private Logger logger = LoggerFactory.getLogger(WebLogAcpect.class);
    /**
     * 定义切入点
     */
    @Pointcut("execution(public * com.geebox.wmstest.controller..*.*(..))")
    public void webLog(){}
 
    /**
     * 前置通知:在连接点之前执行的通知
     * @param joinPoint
     * @throws Throwable
     */
    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 记录下请求内容
        logger.info("URL : " + request.getRequestURL().toString());
        logger.info("HTTP_METHOD : " + request.getMethod());
        logger.info("IP : " + request.getRemoteAddr());
        logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
    }
    @AfterReturning(returning = "ret",pointcut = "webLog()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 处理完请求,返回内容
        logger.info("RESPONSE : " + ret);
    }
}

来源:https://blog.csdn.net/lmb55/article/details/82470388/

常见的切面表达式
1 所有公有方法的执行
execution(public * *(..))
2 所有以set开头的公有方法的执行
execution(* set*(..))
3 AccountService接口下的所有方法的执行
execution(* com.xyz.service.AccountService.*(..))
4 com.xyz.service包下的所有方法的执行
execution(* com.xyz.service.*.*(..))
5 com.xyz.service包及其子包下的所有方法的执行
execution(* com.xyz.service..*.*(..))
6 匹配com.xyz.service包下的所有类的所有方法(不含子包)
within(com.xyz.service.*)
7 com.xyz.service包和子包的所有方法
within(com.xyz.service..*)
8 匹配AccountService的代理类(不支持通配符)
this(com.xyz.service.AccountService)
官方文档
https://docs.spring.io/spring/docs/4.2.5.RELEASE/spring-framework-reference/html/aop.html#aop-schema

aspect中获取注解

//获取方法上面的注解
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
String dsKey = methodSignature.getMethod().getAnnotation(DS.class).value();

切入函数中获取注解

复制代码
    @Around("pointcut() && @annotation(ds)")
    public Object around(ProceedingJoinPoint joinPoint, DS ds) throws Throwable {
        //数据源名称
        String dsKey = ds.value();
        DynamicDataSourceContextHolder.setContextKey(dsKey);
        try {
            return joinPoint.proceed();
        } finally {
            DynamicDataSourceContextHolder.removeContextKey();
        }
    }
复制代码

//Service方法上的注解:ds.value();
//Service方法上的注解:((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(DS.class).value();
//Service类上的注解:joinPoint.getTarget().getClass().getAnnotation(DS.class).value()

posted on   邢帅杰  阅读(172)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2017-12-20 MVC之CodeFirst
点击右上角即可分享
微信分享提示