定义注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnno {
String param();//注解传递的参数,可不加
}
定义切面
@Component
@Aspect
public class MyInterceptor {
@Before(value = "execution(public * com.hc.controller.*Controller.*()) && @annotation(myAnno)")
public void proceed(MyAnno myAnno) throws Throwable {
System.out.println("***********************");
System.out.println(myAnno.param());
}
}
定义Controller
在com.hc.controller包下面定义控制器:
@RestController
public class FunController {
@RequestMapping("/fun1")
public void fun1( ) {
System.out.println("fun1");
}
@MyAnno(param = "ath")
@RequestMapping("/fun2")
public void fun2( ) {
System.out.println("fun2");
}
}