springboot---整合Aop

使用注解的方式

1.编写注解

package com.jiayou.annotation;

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

/**
 * @Auther: yao
 * @Date: 2021/03/04/13:41
 */
@Target({ElementType.METHOD, ElementType.TYPE,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DoneTime {
    String param() default "";
}

2.编写切面类

package com.jiayou.aspect;

import com.jiayou.annotation.DoneTime;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Auther: yao
 * @Date: 2021/03/04/13:37
 */
@Aspect
@Component
public class DoneTimeAspect {

    @Around("@annotation(doneTime)")
    public Object around(ProceedingJoinPoint joinPoint, DoneTime doneTime) throws Throwable {
        System.out.println("方法开始时间是:"+new Date());
        Object o = joinPoint.proceed();
        System.out.println("方法结束时间是:"+new Date()) ;
        return o;
    }
}

3.测试类

 @GetMapping("/findAll")
    @DoneTime
    public String findAll() {
        System.out.println( "方法执了" );

        return "Hello World!";
    }

4.结果

 

posted @ 2021-03-04 15:21  大姚子  阅读(36)  评论(0)    收藏  举报