java开发中常用的注解有哪些

Python014

java开发中常用的注解有哪些,第1张

Java 注解全面解析,学习java做一个java工程师不但待遇高,而且前途无可限量。为什么这样说呢?因为java程序语言作为最流行的计算机开发语言之一,几乎所有的系统、软件、app、网页等都是需要用到java的。

1.基本语法

注解定义看起来很像接口的定义。事实上,与其他任何接口一样,注解也将会编译成class文件。

@Target(ElementType.Method)

@Retention(RetentionPolicy.RUNTIME)

public @interface Test {}

除了@符号以外,@Test的定义很像一个空的接口。定义注解时,需要一些元注解(meta-annotation),如@Target和@Retention

@Target用来定义注解将应用于什么地方(如一个方法或者一个域)

@Retention用来定义注解在哪一个级别可用,在源代码中(source),类文件中(class)或者运行时(runtime)

在注解中,一般都会包含一些元素以表示某些值。当分析处理注解时,程序可以利用这些值。没有元素的注解称为标记注解(marker annotation)

四种元注解,元注解专职负责注解其他的注解,所以这四种注解的Target值都是ElementType.ANNOTATION_TYPE

注解 说明

@Target 表示该注解可以用在什么地方,由ElementType枚举定义

CONSTRUCTOR:构造器的声明

FIELD:域声明(包括enum实例)

LOCAL_VARIABLE:局部变量声明

METHOD:方法声明

PACKAGE:包声明

PARAMETER:参数声明

TYPE:类、接口(包括注解类型)或enum声明

ANNOTATION_TYPE:注解声明(应用于另一个注解上)

TYPE_PARAMETER:类型参数声明(1.8新加入)

TYPE_USE:类型使用声明(1.8新加入)

PS:当注解未指定Target值时,此注解可以使用任何元素之上,就是上面的类型

@Retention 表示需要在什么级别保存该注解信息,由RetentionPolicy枚举定义

SOURCE:注解将被编译器丢弃(该类型的注解信息只会保留在源码里,源码经过编译后,注解信息会被丢弃,不会保留在编译好的class文件里)

CLASS:注解在class文件中可用,但会被VM丢弃(该类型的注解信息会保留在源码里和class文件里,在执行的时候,不会加载到虚拟机(JVM)中)

RUNTIME:VM将在运行期也保留注解信息,因此可以通过反射机制读取注解的信息(源码、class文件和执行的时候都有注解的信息)

PS:当注解未定义Retention值时,默认值是CLASS

@Documented 表示注解会被包含在javaapi文档中

@Inherited 允许子类继承父类的注解

2. 注解元素

– 注解元素可用的类型如下:

– 所有基本类型(int,float,boolean,byte,double,char,long,short)

– String

– Class

– enum

– Annotation

– 以上类型的数组

如果使用了其他类型,那编译器就会报错。也不允许使用任何包装类型。注解也可以作为元素的类型,也就是注解可以嵌套。

元素的修饰符,只能用public或default。

– 默认值限制

编译器对元素的默认值有些过分挑剔。首先,元素不能有不确定的值。也就是说,元素必须要么具有默认值,要么在使用注解时提供元素的值。

其次,对于非基本类型的元素,无论是在源代码中声明,还是在注解接口中定义默认值,都不能以null作为值。这就是限制,这就造成处理器很难表现一个元素的存在或缺失状态,因为每个注解的声明中,所有的元素都存在,并且都具有相应的值。为了绕开这个限制,只能定义一些特殊的值,例如空字符串或负数,表示某个元素不存在。

@Target(ElementType.Method)

@Retention(RetentionPolicy.RUNTIME)

public @interface MockNull {

public int id() default -1

public String description() default “”

}

3. 快捷方式

何为快捷方式呢?先来看下springMVC中的Controller注解

@Target({ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Component

public @interface Controller {

String value() default “”

}

可以看见Target应用于类、接口、注解和枚举上,Retention策略为RUNTIME运行时期,有一个String类型的value元素。平常使用的时候基本都是这样的:

@Controller(“/your/path”)

public class MockController { }

这就是快捷方式,省略了名-值对的这种语法。下面给出详细解释:

注解中定义了名为value的元素,并且在应用该注解的时候,如果该元素是唯一需要赋值的一个元素,那么此时无需使用名-值对的这种语法,而只需在括号内给出value元素所需的值即可。这可以应用于任何合法类型的元素,当然了,这限制了元素名必须为value。

4. JDK1.8注解增强

TYPE_PARAMETER和TYPE_USE

在JDK1.8中ElementType多了两个枚举成员,TYPE_PARAMETER和TYPE_USE,他们都是用来限定哪个类型可以进行注解。举例来说,如果想要对泛型的类型参数进行注解:

public class AnnotationTypeParameter<@TestTypeParam T>{}

那么,在定义@TestTypeParam时,必须在@Target设置ElementType.TYPE_PARAMETER,表示这个注解可以用来标注类型参数。例如:

@Target(ElementType.TYPE_PARAMETER)

@Retention(RetentionPolicy.RUNTIME)

public @interface TestTypeParam {}

ElementType.TYPE_USE用于标注各种类型,因此上面的例子也可以将TYPE_PARAMETER改为TYPE_USE,一个注解被设置为TYPE_USE,只要是类型名称,都可以进行注解。例如有如下注解定义:

@Target(ElementType.TYPE_USE)

@Retention(RetentionPolicy.RUNTIME)

public @interface Test {}

那么以下的使用注解都是可以的:

List<@Test Comparable>list1 = new ArrayList<>()

List<? extends Comparable>list2 = new ArrayList<@Test Comparable>()

@Test String text

text = (@Test String)new Object()

java.util. @Test Scanner console

console = new java.util.@Test Scanner(System.in)

PS:以上@Test注解都是在类型的右边,要注意区分1.8之前的枚举成员,例如:

@Test java.lang.String text

在上面这个例子中,显然是在进行text变量标注,所以还使用当前的@Target会编译错误,应该加上ElementType.LOCAL_VARIABLE。

@Repeatable注解

@Repeatable注解是JDK1.8新加入的,从名字意思就可以大概猜出他的意思(可重复的)。可以在同一个位置重复相同的注解。举例:

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

public @interface Filter {

String [] value()

}

如下进行注解使用:

@Filter({“/admin”,”/main”})

public class MainFilter { }

换一种风格:

@Filter(“/admin”)

@Filter(“/main”)

public class MainFilter {}

在JDK1.8还没出现之前,没有办法到达这种“风格”,使用1.8,可以如下定义@Filter:

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

@Repeatable(Filters.class)

public @interface Filter {

String value()

}

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

public @interface Filters {

Filter [] value()

}

实际上这是编译器的优化,使用@Repeatable时告诉编译器,使用@Filters来作为收集重复注解的容器,而每个@Filter存储各自指定的字符串值。

JDK1.8在AnnotatedElement接口新增了getDeclaredAnnotationsByType和getAnnotationsByType,在指定@Repeatable的注解时,会寻找重复注解的容器中。相对于,getDeclaredAnnotation和getAnnotation就不会处理@Repeatable注解。举例如下:

@Filter(“/admin”)

@Filter(“/filter”)

public class FilterClass {

public static void main(String[] args) {

Class<FilterClass>filterClassClass = FilterClass.class

Filter[] annotationsByType = filterClassClass.getAnnotationsByType(Filter.class)

if (annotationsByType != null) {

for (Filter filter : annotationsByType) {

System.out.println(filter.value())

}

}

System.out.println(filterClassClass.getAnnotation(Filter.class))

}

}

日志如下:

/admin

/filter

null

望采纳!

这个是没有实现的。

解释:注释本身具有针对性,也就是注释是自定义的,根据实际需要可以修改,此类内容并没有固定的内容规范,所以是没办法进行注释继承的,只能继承方法,不能继承注释的。

整个继承层次结构中的父类和子类的所有属性都映射到同一个表中, 他们的实例通过一个辨别符(discriminator)列来区分.:

//父类

@Entity

//继承映射成一张表的表结构是 父类字段+子类字段+一个鉴别字段(因为可以有多个子类继承父类,这个鉴别字段用于分别一条数据记录是属于那个子类的)

@Inheritance(strategy=InheritanceType.SINGLE_TABLE)

//鉴别字段 name="鉴别字段名称" discriminatorType 鉴别字段数据类型

@DiscriminatorColumn(

name="planetype",

discriminatorType=DiscriminatorType.STRING

扩展资料:

Annotation(注解)是JDK1.5及以后版本引入的。它可以用于创建文档,跟踪代码中的依赖性,甚至执行基本编译时检查。注解是以‘@注解名’在代码中存在的,根据注解参数的个数,可以将注解分为:标记注解、单值注解、完整注解三类。

都不会直接影响到程序的语义,只是作为注解(标识)存在,可以通过反射机制编程实现对这些元数据(用来描述数据的数据)的访问。另外,可以在编译时选择代码里的注解是否只存在于源代码级,或者它也能在class文件、或者运行时中出现(SOURCE/CLASS/RUNTIME)。

参考资料来源:百度百科-Java 注解

注解的使用一般是与java的反射一起使用,下面是一个例子

注解相当于一种标记,在程序中加了注解就等于为程序打上了某种标记,没加,则等于没有某种标记,以后,javac编译器,开发工具和其他程序可以用反射来了解你的类及各种元素上有无何种标记,看你有什么标记,就去干相应的事。标记可以加在包,类,字段,方法,方法的参数以及局部变量上。

自定义注解及其应用

1)、定义一个最简单的注解

public @interface MyAnnotation {

//......

}

2)、把注解加在某个类上:

@MyAnnotation

public class AnnotationTest{

//......

}

以下为模拟案例

自定义注解@MyAnnotation

1 package com.ljq.test

2

3 import java.lang.annotation.ElementType

4 import java.lang.annotation.Retention

5 import java.lang.annotation.RetentionPolicy

6 import java.lang.annotation.Target

7

8 /**

9 * 定义一个注解

10 *

11 *

12 * @author jiqinlin

13 *

14 */

15 //Java中提供了四种元注解,专门负责注解其他的注解,分别如下

16

17 //@Retention元注解,表示需要在什么级别保存该注释信息(生命周期)。可选的RetentionPoicy参数包括:

18 //RetentionPolicy.SOURCE: 停留在java源文件,编译器被丢掉

19 //RetentionPolicy.CLASS:停留在class文件中,但会被VM丢弃(默认)

20 //RetentionPolicy.RUNTIME:内存中的字节码,VM将在运行时也保留注解,因此可以通过反射机制读取注解的信息

21

22 //@Target元注解,默认值为任何元素,表示该注解用于什么地方。可用的ElementType参数包括

23 //ElementType.CONSTRUCTOR: 构造器声明

24 //ElementType.FIELD: 成员变量、对象、属性(包括enum实例)

25 //ElementType.LOCAL_VARIABLE: 局部变量声明

26 //ElementType.METHOD: 方法声明

27 //ElementType.PACKAGE: 包声明

28 //ElementType.PARAMETER: 参数声明

29 //ElementType.TYPE: 类、接口(包括注解类型)或enum声明

30

31 //@Documented将注解包含在JavaDoc中

32

33 //@Inheried允许子类继承父类中的注解

34

35

36 @Retention(RetentionPolicy.RUNTIME)

37 @Target({ElementType.METHOD, ElementType.TYPE})

38 public @interface MyAnnotation {

39 //为注解添加属性

40 String color()

41 String value() default "我是林计钦"//为属性提供默认值

42 int[] array() default {1, 2, 3}

43 Gender gender() default Gender.MAN//添加一个枚举

44 MetaAnnotation metaAnnotation() default @MetaAnnotation(birthday="我的出身日期为1988-2-18")

45 //添加枚举属性

46

47 }

注解测试类AnnotationTest

1 package com.ljq.test

2

3 /**

4 * 注解测试类

5 *

6 *

7 * @author jiqinlin

8 *

9 */

10 //调用注解并赋值

11 @MyAnnotation(metaAnnotation=@MetaAnnotation(birthday = "我的出身日期为1988-2-18"),color="red", array={23, 26})

12 public class AnnotationTest {

13

14 public static void main(String[] args) {

15 //检查类AnnotationTest是否含有@MyAnnotation注解

16 if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){

17 //若存在就获取注解

18 MyAnnotation annotation=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class)

19 System.out.println(annotation)

20 //获取注解属性

21 System.out.println(annotation.color())

22 System.out.println(annotation.value())

23 //数组

24 int[] arrs=annotation.array()

25 for(int arr:arrs){

26 System.out.println(arr)

27 }

28 //枚举

29 Gender gender=annotation.gender()

30 System.out.println("性别为:"+gender)

31 //获取注解属性

32 MetaAnnotation meta=annotation.metaAnnotation()

33 System.out.println(meta.birthday())

34 }

35 }

36 }

枚举类Gender,模拟注解中添加枚举属性

1 package com.ljq.test

2 /**

3 * 枚举,模拟注解中添加枚举属性

4 *

5 * @author jiqinlin

6 *

7 */

8 public enum Gender {

9 MAN{

10 public String getName(){return "男"}

11 },

12 WOMEN{

13 public String getName(){return "女"}

14 }//记得有“”

15 public abstract String getName()

16 }

注解类MetaAnnotation,模拟注解中添加注解属性

1 package com.ljq.test

2

3 /**

4 * 定义一个注解,模拟注解中添加注解属性

5 *

6 * @author jiqinlin

7 *

8 */

9 public @interface MetaAnnotation {

10 String birthday()

11 }