java自定义注解实现切面

自定义注解实现切面

自定义注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
// 注解使用于方法上
@Target(ElementType.METHOD)
public @interface InvokeAnnotation {
    
    
	// 自定义参数
    String value() default "参数1";
    // 自定义参数
    String description() default "参数2";
}

切面方法

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Aspect
@Component
@Slf4j
public class InvokeAspect {
    
    


	// 注解全路径
    @Pointcut("@annotation(**.InvokeAnnotation)")
    public void invoke() {
    
    
    }

    @Around("invoke() && @annotation(invokeAnnotation)")
    public Object invokeAfter(ProceedingJoinPoint joinPoint, InvokeAnnotation invokeAnnotation) throws Throwable {
    
    

        // 执行结果
        return joinPoint.proceed();
    }

}

猜你喜欢

转载自blog.csdn.net/qq_41995299/article/details/129862043