zoukankan      html  css  js  c++  java
  • SpringBoot进阶教程(六十五)自定义注解

    在上一篇文章《SpringBoot进阶教程(六十四)注解大全》中介绍了springboot的常用注解,springboot提供的注解非常的多,这些注解简化了我们的很多操作。今天主要介绍介绍自定义注解。

    自spring4.0开放以来,自定义注解非常常见,项目中都会或多或少的使用自定义注解,我们的demo中主要针对登录校验来介绍如何量身定制自定义注解。

    v项目结构

    SpringBoot进阶教程(六十四)自定义注解

    v定义注解

    package learn.web.controller.interceptor;
    
    import java.lang.annotation.Documented;
    import java.lang.annotation.Retention;
    import java.lang.annotation.Target;
    
    import static java.lang.annotation.ElementType.METHOD;
    import static java.lang.annotation.RetentionPolicy.RUNTIME;
    
    /**
     * @author toutou
     * @date by 2020/11
     * @des
     */
    @Documented
    @Retention(RUNTIME)
    @Target(METHOD)
    public @interface Login {
    }

    元注解释义:

    java.lang.annotation提供了四种元注解,专门注解其他的注解(在自定义注解的时候,需要使用到元注解):

    @Documented:注解是否将包含在JavaDoc中

    @Retention:什么时候使用该注解,指明修饰的注解的生存周期,即会保留到哪个阶段。

    1.RetentionPolicy.SOURCE —— 这种类型的Annotations只在源代码级别保留,编译时就会被忽略
    2.RetentionPolicy.CLASS —— 这种类型的Annotations编译时被保留,在class文件中存在,但JVM将会忽略
    3.RetentionPolicy.RUNTIME —— 这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用

    @Target:注解用于什么地方,指明了修饰的这个注解的使用范围,即被描述的注解可以用在哪里。

    1.TYPE——用于描述类、接口(包括注解类型) 或enum声明
    2.FIELD——用于字段声明(包括枚举常量)
    3.METHOD——用于方法声明
    4.PARAMETER——用于参数声明
    5.CONSTRUCTOR——用于构造函数声明
    6.LOCAL_VARIABLE——用于本地变量声明
    7.ANNOTATION_TYPE——用于注解类型声明
    8.PACKAGE——用于包声明
    9.TYPE_PARAMETER—— 用于类型参数声明,JavaSE8引进,可以应用于类的泛型声明之处
    10.TYPE_USE——JavaSE8引进,此类型包括类型声明和类型参数声明,是为了方便设计者进行类型检查,例如,如果使用@Target(ElementType.TYPE_USE)对@NonNull进行标记,则类型检查器可以将@NonNull class C {...} C类的所有变量都视为非null

    @Inherited:是否允许子类继承该注解

    v实现注解

    /**
     * @author toutou
     * @date by 2020/11
     * @des
     */
    @Component
    public class LoginInterceptor extends HandlerInterceptorAdapter {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            System.out.println("Interceptor Interceptor Interceptor Interceptor Interceptor Interceptor Interceptor");
            if (handler instanceof HandlerMethod) {
                HandlerMethod handlerMethod = (HandlerMethod) handler;
                Method method = handlerMethod.getMethod();
                Login login = method.getAnnotation(Login.class);
                if(login!=null) {
                    // login!=null表示@Login注解生效
                    if(!"user".equals(request.getParameter("user"))){
                        new RuntimeException("没有登录.");
                    }
                }
            }
    
            return true;
        }
    }

    v配置拦截器

    package learn.web.controller.config;
    
    import learn.web.controller.interceptor.LoginInterceptor;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    
    /**
     * @author toutou
     * @date by 2020/11
     * @des
     */
    @Configuration
    public class WebMvcConfig extends DelegatingWebMvcConfiguration {
        @Autowired
        LoginInterceptor loginInterceptor;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(loginInterceptor);
            super.addInterceptors(registry);
            System.out.println("Interceptor Interceptor Interceptor Interceptor Interceptor Interceptor Interceptor");
        }
    }

    v使用注解

    @RestController
    public class UserController {
        @GetMapping("/user/hello")
        @Login
        public String helloWorld() {
            return "hello world.";
        }
    }

    v源码地址

    https://github.com/toutouge/javademosecond/tree/master/hellolearn

    v博客总结

    写到这才发现,项目结构目录有点小问题,interceptor和config两个包应该是和controller平行的,而不是controller的子目录。即:package learn.web.controller.interceptor改成package learn.web.interceptor;learn.web.controller.config改成learn.web.config。原文就懒得重新捯饬了,大家注意下就行,当然原文也不会有问题,只是目录结构改一下会更好。


    作  者:请叫我头头哥
    出  处:http://www.cnblogs.com/toutou/
    关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!
    版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
    特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信
    声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!

  • 相关阅读:
    [编]在Web站点中创建和使用Rss源
    Command 模式 Step by Step
    正则表达式 教程
    (转)mysql处理高并发,防止库存超卖
    【转】Golang- import 导入包的几种方式:点,别名与下划线
    win10 c++ build tools的安装
    Reporting Services VS designer 的一个 bug
    使用 AppDomain 让不支持线程安全的代码轻松支持线程安全
    应该怎样设计和开发软件
    Razor 也可说是一个模板引擎,用不着学习 T4 了
  • 原文地址:https://www.cnblogs.com/toutou/p/spring_boot_annotation.html
Copyright © 2011-2022 走看看