版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39053211/article/details/84928589
package com.imooc.boot.controller;
import com.imooc.boot.aspect.IndexAspect;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("member")
public class MemberController {
@IndexAspect
@RequestMapping(value = "/index",method = RequestMethod.GET)
public String index(){
System.out.println("aspect test after");
return "SUCCESS";
}
}
package com.imooc.boot.aspect;
import java.lang.annotation.*;
/**
* 自定义的注解
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface IndexAspect {
}
package com.imooc.boot.aspect;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* 定义切面类
*/
@org.aspectj.lang.annotation.Aspect
@Component
public class Aspect {
/**
* 定义切点-->自定义注解
*/
@Pointcut("@annotation(com.imooc.boot.aspect.IndexAspect)")
public void afterAsperct(){
}
/**
* 具体在切点前面前或者后需要执行的流程,
* 在此只写了在切点前执行
* -->切点后执行的话 @After 相关的注解,
*/
@Before("afterAsperct()")
public void before(){
System.out.println("切面前执行-->");
}
}