1. 程式人生 > >Spring MVC 自定義註解

Spring MVC 自定義註解

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()); } }