学习:springMVC注解
引言
在項目中,組長說我們的@Autowired注解都是黃的
后來,組長說加上@SuppressWarnings來抑制警告信息
-
@SuppressWarnings 注解目標
其注解目標為類、字段、函數、函數入參、構造函數和函數的局部變量。
了解spring注解
spring注解分為兩大類:
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注解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ Testing Framewor
- 下一篇: 深入理解DOM事件机制