spring aop

Last updated on November 20, 2024 am

🧙 Questions

通过spring aop注入,实现监听service整个生命周期。

☄️ Ideas

添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
implementation 'org.springframework.boot:spring-boot-starter-aop'
创建注解WatchDog

建议在 annotation 目录下创建注解

@Target(value = {ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface WatchDog {

    String who() default "ispong";
}
解析注解

通常以 Aspect 为命名结尾

@Aspect
@Component
@Slf4j
public class WatchDogAspect {

    /*
     * 指定需要解析的注解
     *
     * @ispong
     */
    @Pointcut("@annotation(com.isxcode.leo.demo.spring.aop.annotation.WatchDog)")
    public void watchDogOperate() {

    }

    @Before(value = "watchDogOperate()&&@annotation(watchDog)")
    public void before(JoinPoint joinPoint, WatchDog watchDog) {

        log.info("2:before");
        String username = spelParseUsername(joinPoint, watchDog.who());
        log.info("3:username {}", username);
    }

    @AfterReturning(value = "watchDogOperate()&&@annotation(watchDog)")
    public void afterReturning(JoinPoint joinPoint, WatchDog watchDog) {

        log.info("5:afterReturning");
    }

    @AfterThrowing(value = "watchDogOperate()&&@annotation(watchDog)")
    public void afterThrowing(JoinPoint joinPoint, WatchDog watchDog) {

        log.info("afterThrowing");
    }

    @After(value = "watchDogOperate()&&@annotation(watchDog)")
    public void after(JoinPoint joinPoint, WatchDog watchDog) {

        log.info("6:after");
    }

    @Around(value = "watchDogOperate()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {

        log.info("1:around");
        // 开始执行service中的方法
        Object retVal = pjp.proceed();
        log.info("7:around");
        return retVal;
    }

    /*
     * spring表达式解析
     *
     * @ispong
     */
    public String spelParseUsername(JoinPoint joinPoint, String expressionStr) {

        if (expressionStr.isEmpty()) {
            return "";
        }

        ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext();

        String[] params = new LocalVariableTableParameterNameDiscoverer().getParameterNames(((MethodSignature) joinPoint.getSignature()).getMethod());
        if (params == null || params.length < 1) {
            return "";
        }

        for (String paramName : params) {
            context.setVariable(paramName, joinPoint.getArgs()[paramName.indexOf(paramName)]);
        }

        return (String) parser.parseExpression(expressionStr).getValue(context);
    }
}
请求体
@Data
public class ReqDto {

    private String username;
}
注解使用
@Slf4j
@Component
public class DogService {

    /*
     * 支持动态传参
     *
     * @ispong
     */
    @WatchDog(who = "#reqDto.username")
    public void testWatchDog(ReqDto reqDto) {

        log.info("4:reqDto {}", reqDto);
    }
}

20220927200015


spring aop
https://ispong.isxcode.com/spring/spring/spring aop/
Author
ispong
Posted on
May 19, 2021
Licensed under