日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

学习:springMVC注解

發布時間:2025/5/22 c/c++ 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 学习:springMVC注解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

引言

在項目中,組長說我們的@Autowired注解都是黃的

后來,組長說加上@SuppressWarnings來抑制警告信息

  • @SuppressWarnings 注解目標

    其注解目標為類、字段、函數、函數入參、構造函數和函數的局部變量。
第一次使用其注解,發現spring的強大,并了解了一下spring的各種注解

了解spring注解

spring注解分為兩大類:

  • spring的bean容器相關的注解,或者說bean工廠相關的注解;
  • 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從任何一個地方都能體現它的強大之處,但是我們呢??

    總結

    以上是生活随笔為你收集整理的学习:springMVC注解的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。