java怎么写注解

Java中其实内置的注解并不多,我们平时多是使用一些框架的注解,比如Spring里的@Controller,@RequsetMapping等等,那注解到底是什么?它是怎么工作的?

注解其实是给框架或者系统看的,它本身并没有什么用,只是告诉框架,这个被注解标记的类,方法或者属性需要框架去做什么.然后框架在读取了这个注解以后,就会对这个注解所标记的类,方法,属性进行一顿操作.

@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public@interfaceTest { Class<? extends Throwable> expected //www.58yuanyou.comdefaultTest.None.class;

longtimeoutdefault0L;

publicstaticclassNoneextendsThrowable{privatestaticfinallongserialVersionUID = 1L;

privateNone{}}}

比如上面的@Test注解,就是告诉junit这个测试框架,这www.58yuanyou.com是一个测试的方法,点开它的源码,我们发现,他是被@interface来注明的,像类用class来注明一样,它上面还有两个注解一个@Retention和@Target.

@Target是用来表明注解是在哪里放的,是放在方法上,还是放在类上,还是属性上,下面例子中,ElementType.METHOD就是说@Test这个注解是放在方法上的.点进ElementType,这是一个枚举类型,代表了@Target可以取的值,也就是可以放在什么上面,

  • 原由网
publicenum 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** @since1.8*/TYPE_PARAMETER,

/*** Use of a type** @since1.8*/TYPE_USE}

@Retention则用来表示该注解生存期,RetentionPolicy.RUNTIME也很好理解,是在运行时生存的.RetentionPolicy也是一个枚举类型,分别有三个值,源码级别(source),类文件级别(class)或者运行时级别(runtime),具体说明源码里有英文,自己看吧~

publicenumRetentionPolicy{/*** 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}

注解已经简单的说明了一下,那怎么用呢?只有我们自己用过一遍,才会知道框架提供给我们的注解的原理是怎样的.

Talk is cheap ,show me the code~

//这样就创建了一个注解,可是这个注解里面什么都没有,//但是的确可以用了,虽然什么作用都没有public@interfaceDaMao{}
@DaMao//这里直接在别的地方就能写我的注解了publicclassApp{publicstaticvoidmain(String[] args ){System.out.println( "Hello World!");}}
//让注解在运行时保留,作用在类上@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)public@interfaceDaMao {//给我们的注解写上属性,可以有默认值,// 也就是当我们没有给值的时候,属性默认是多少int id default0;String name default"孙";}

好了,我们自己简单的写了一个注解,要使用的时候就直接加在我们定义的使用位置就好了,但是还需要我们去解析注解了,因为你虽然把注解的定义好了,但是注解的东西在那里放着,机器并不知道你注解里的东西到底是什么,你必须亲自拿出来,然后给机器,机器才能够读明白你的注解.

怎么把注解拿出来,把内容读给机器呢?这里就需要用到反射

  • //www.58yuanyou.com
@DaMaopublicclassApp{publicstaticvoid main(String[] args) {//通过 Class 对象的 isAnnotationPresent 方法判断它是否应用了某个注解boolean hasAnnotation = App.class.isAnnotationPresent(DaMao.class);if( hasAnnotation ) {DaMaodaMaoAnnotation = App.class.getAnnotation(DaMao.class);//有注解就获取这个注解System.out.println("id:"+daMaoAnnotation.id);//拿到注解里的内容System.out.println("name:"+daMaoAnnotation.name);}}}

结果

java怎么写注解

现在我们可以知道,一个注解的完整使用过程,先定义好,然后标记在需要标记的位置,最后要解析.

像Spring中的注解也是同样的道原由网理,Spring定义了许多的注解,然后他内部又对这些注解进行了解析,我们只需要放在我们需要放且Spring规定放的位置,那么当我们的程序运行起来的时候,注解就能正常的工作了.

内容版权声明:除非注明原创否则皆为转载,再次转载请注明出处。

文章标题: java怎么写注解

文章地址: www.58yuanyou.com/jiqiao/84481.html

相关推荐