一: 内置注解:
-
@Override 重写的注解,十分常见
-
@Deprecated 废弃类,被标注的元素一般是不推荐程序员使用的内容,当然,也可能是说比起该方法,java存在其他更优秀的选择
常见的方法有Date,如果用Date实例出的对象去调用其相关方法的话就会看到方法中间存在斜线,点进去查看源码的时候就能发现上方存在@Deprecated注解,当然,不推荐使用不代表不可以使用源码:
-
@SuppressWarnings 镇压警告,定义在java.lang.SuppressWarnings类中,可以抑制编译时的报错信息。
比如,直接在java中定义了一个没被使用的变量,编译器会报错,提示你变量未被使用,在Eclipse中会出现左侧警示符,变量下出现淡黄波浪线,IDEA中则变量会变色,该注解支持作用于在类。加注解,当然注解需要一个参数,选择镇压全部即不再出现警告提示
二:元注解
元注解负责注解其他注解
- @Target 用于描述注解的使用范围
参考Override的注解源码,可以看到其范围为METHOD,说明作用在方法上,因此我们不会把Override加在整个类上。
而像@SuppressWarnings注解,其作用范围就相对广泛了。
至于@Target中传入的参数,我们打开其源码。可以看到@Target中传入的是一个ElementType
接着进入ElementType源码,参数的详细信息已经在源码中介绍了。
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
}
-
@Retention 表示在什么级别保存该注释信息,描述注解生命周期(SOURCE<CLASS<RUNTIME)
例如@Override源码,其运行级别就是SOURCE源码级,也就是说是给java代码看的,而java程序跑起来想要实现业务逻辑的时候是不会管你是不是override的的,它需要的只是找方法然后能实现功能就行了,也就是运行RUNTIME级别。
运行级别的列子比如RequestMapping,如果让程序在运行的时候找不到RequestMapping定义的注解,那接口的存在就没意义了
-
@Documented 说明该注解被包含在javadoc中
-
@Inherited 说明子类可以继承父类中的该注解
三:自定义注解
使用@interface可以自动继承java.lang.annotation.Annotation接口,格式:@interface 注解名 {内容}
简单示例:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class test01 {
@MyAnnoation
public void test() {
}
}
//定义一个注解
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnoation{
}
有参注解:
注意定义的注解中的内容为参数而不是方法,同时可以使用default方法来为注解指定默认值,如果不指定,那注解中此参数必填,如果存在多参数,填写的参数没有顺序要求。
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class test02 {
@PersonAnnotation(names = { "admin,admin1" })
public void test() {
}
}
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface PersonAnnotation{
//这是注解的参数,不是一个方法 default ""表示参数默认值为空,因此在使用注解的时候可以不写name参数
String name() default "";
String[] names();
}
唯一value参注解:
假设注解中只有一个参数,且参数名固定是value,那即便是value本身没有用default指定默认值,依然可以在使用注解的时候直接指定,而无需重新申明参数。
例如@SuppressWarnings注解源码:
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
/**
* The set of warnings that are to be suppressed by the compiler in the
* annotated element. Duplicate names are permitted. The second and
* successive occurrences of a name are ignored. The presence of
* unrecognized warning names is <i>not</i> an error: Compilers must
* ignore any warning names they do not recognize. They are, however,
* free to emit a warning if an annotation contains an unrecognized
* warning name.
*
* <p> The string {@code "unchecked"} is used to suppress
* unchecked warnings. Compiler vendors should document the
* additional warning names they support in conjunction with this
* annotation type. They are encouraged to cooperate to ensure
* that the same names work across multiple compilers.
* @return the set of warnings to be suppressed
*/
String[] value();
}
唯一参数value,那如下两种写法都可以: