zoukankan      html  css  js  c++  java
  • 自定义注解及应用

    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface Login {
    }
    1.注解的定义:Java文件叫做Annotation,用@interface表示。
    2.元注解:@interface上面按需要注解上一些东西,包括@Retention、@Target、@Document、@Inherited四种。
    3.注解的保留策略:
      @Retention(RetentionPolicy.SOURCE)   // 注解仅存在于源码中,在class字节码文件中不包含
      @Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得
      @Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
    4.注解的作用目标:
      @Target(ElementType.TYPE)                     // 接口、类、枚举、注解
      @Target(ElementType.FIELD)                     // 字段、枚举的常量
      @Target(ElementType.METHOD)                 // 方法
      @Target(ElementType.PARAMETER)            // 方法参数
      @Target(ElementType.CONSTRUCTOR)       // 构造函数
      @Target(ElementType.LOCAL_VARIABLE)   // 局部变量
      @Target(ElementType.ANNOTATION_TYPE) // 注解
      @Target(ElementType.PACKAGE)               //
    5.注解包含在javadoc中:
      @Documented
    6.注解可以被继承:
      @Inherited
    7.注解解析器:用来解析自定义注解。
     
    应用:
    拦截器应用
    public class AuthorizationInterceptor extends HandlerInterceptorAdapter {
        @Autowired
        private TokenService tokenService;
        public static final String USER_KEY = "userId";
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            Login annotation;
            if(handler instanceof HandlerMethod) {
                annotation = ((HandlerMethod) handler).getMethodAnnotation(Login.class);
            }else{
                return true;
            }
            if(annotation == null){
                return true;
            }
    参数解析器应用:
    
    public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
        @Override
        public boolean supportsParameter(MethodParameter parameter) {
            return parameter.getParameterType().isAssignableFrom(UserEntity.class) && parameter.hasParameterAnnotation(LoginUser.class);
        }
        @Override
        public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
                                      NativeWebRequest request, WebDataBinderFactory factory) throws Exception {
            //获取用户ID
            Object object = request.getAttribute(AuthorizationInterceptor.USER_KEY, RequestAttributes.SCOPE_REQUEST);
            if(object == null){
                return null;
            }
            //获取用户信息
            UserEntity user = userService.selectById((Long)object);
            return user;
        }
  • 相关阅读:
    山东理工大学ACM平台题答案关于C语言 1137 C/C++经典程序训练7---求某个范围内的所有素数
    又遇BUG-ORA-01148:数据文件忽然变为recover状态
    poj 1191 棋盘分割 动态规划
    libevent的使用方法--回显服务器的简单实例
    java.lang.OutOfMemory总结分析
    山东理工大学ACM平台题答案关于C语言 1580 闰年
    SAE搭建WordPress教程 免费建WordPress博客站
    编译小结(6)认识Automake
    PHP再学习1——cURL表单提交、HTTP请求和响应分析
    新手一步一步OpenCV+Win7+Visual Studio 2013环境配置
  • 原文地址:https://www.cnblogs.com/bchange/p/9448004.html
Copyright © 2011-2022 走看看