学习:springMVC注解

栏目: Java · 发布时间: 5年前

内容简介:在项目中,组长说我们的后来,组长说加上

引言

在项目中,组长说我们的 @Autowired 注解都是黄的

学习:springMVC注解

后来,组长说加上 @SuppressWarnings 来抑制警告信息

学习:springMVC注解

  • @SuppressWarnings 注解目标

    其注解目标为类、字段、函数、函数入参、构造函数和函数的局部变量。

第一次使用其注解,发现spring的强大,并了解了一下spring的各种注解

了解spring注解

spring注解分为两大类:

  1. spring的bean容器相关的注解,或者说bean工厂相关的注解;
  2. springmvc相关的注解。

spring的bean容器相关的注解:先后有: @Required@Autowired@PostConstruct@PreDestory ,还有Spring3.0开始支持的JSR-330标准javax.inject.*中的注解( @Inject@Named@Qualifier , @Provider , @Scope , @Singleton ).

springmvc相关的注解有: @Controller , @RequestMapping , @RequestParam@ResponseBody 等等。

springMVC注解

  • @Override

我们最熟悉的 @Override, 定义

``
/**
 * Indicates that a method declaration is intended to override a      
 * method declaration in a supertype. If a method is annotated with
 * this annotation type compilers are required to generate an error
 * message unless at least one of the following conditions hold:
 * The method does override or implement a method declared in a
 * supertype.
 * The method has a signature that is override-equivalent to that of
 * any public method declared in Object.
 *  * @author  Peter von der Ahé
 * @author  Joshua Bloch
 * @jls 9.6.1.4 @Override
 * @since 1.5
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
```

从注释,我们可以看出, @Override 的作用是,提示编译器,使用了 @Override 注解的方法必须 override 父类或者 java.lang.Object 中的一个同名方法。

我们看到 @Override 的定义中使用到了 @Target , @Retention ,它们就是所谓的 元注解 ——就是定义注解的注解

  • @Retention

@Retention 用于提示注解被保留多长时间,有三种取值:

public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,
    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,
    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

RetentionPolicy.SOURCE 保留在源码级别,编译时忽略(@Override就是此类);

RetentionPolicy.CLASS 被编译器保留在编译后的类文件级别,但是在运行丢弃;

RetentionPolicy.RUNTIME 保留至运行时。

  • @Target

@Target :注解的作用目标

public enum ElementType {
    /** Class, interface (including annotation type), or enum declaration */   //接口、类、枚举、注解
    TYPE,
    /** Field declaration (includes enum constants) */                         //字段、枚举的常量
    FIELD,
    /** Method declaration */                                                  //方法
    METHOD,
    /** Formal parameter declaration */                                        //方法参数
    PARAMETER,
    /** Constructor declaration */                                             //构造函数 
    CONSTRUCTOR,
    /** Local variable declaration */                                          //局部变量
    LOCAL_VARIABLE,
    /** Annotation type declaration */                                         //注解类型
    ANNOTATION_TYPE,
    /** Package declaration */                                                 //包
    PACKAGE,
    /**
     * Type parameter declaration
     * @since 1.8
     */
    TYPE_PARAMETER,
    /**
     * Use of a type
     * @since 1.8
     */
    TYPE_USE
}

举例

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface TestOnly {
}

只能使用在方法上,保留在源码级别,编译时忽略

总结

注解是用于建设基础jar包的一部分,项目都有自己的框架,若运用恰当,注解则为其中良好的一部分;spring从任何一个地方都能体现它的强大之处,但是我们呢??


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据结构基础

数据结构基础

[美]Ellis Horowitz 霍罗维兹 / 朱仲涛 / 清华大学出版社 / 2009-3 / 49.00元

《数据结构基础(C语言版)(第2版)》是最经典数据结构教材的最新版本,国内外大多数的同类教材都是以《数据结构基础(C语言版)(第2版)》为蓝本编写而来的。《数据结构基础(C语言版)(第2版)》用C作为描述语言,全面而生动地介绍了数据结构的有关知识,如数组、栈、队列、链表、树和图,以及构成所有软件基础的排序散列技术。此外,《数据结构基础(C语言版)(第2版)》还介绍了各种高级或特殊数据结构,如优先级......一起来看看 《数据结构基础》 这本书的介绍吧!

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具