Spring MVC 自定義註解
阿新 • • 發佈:2019-01-02
1. 編寫註解
@Target({ElementType.METHOD})
@Retention(RUNTIME)
public @interface Demo {
String value() default "";
}
2. 編寫攔截器
public class DemoInteceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HandlerMethod methodHandler = (HandlerMethod) handler;
Demo demo = methodHandler.getMethodAnnotation(Demo.class);
System.out.println(demo.value());
}
}