java注解
注解:
从 JDK5 开始,Java 增加对元数据的支持,也就是注解,注解与注释是有一定区别的,可以把注解理解为代码里的特殊标记,这些标记可以在编译,类加载,运行时被读取,并执行相应的处理。通过注解开发人员可以在不改变原有代码和逻辑的情况下在源代码中嵌入补充信息。SSM 项目中存在各种注解,因为后续会手写这几个框架,所有需要先打好基础,将这几个框架中要用到的知识点写学习。
注解的定义
注解通过 @interface
关键字进行定义。
public @interface TestAnnotation {String value() </span><span style="color: rgba(0, 0, 255, 1)">default</span> ""<span style="color: rgba(0, 0, 0, 1)">;
}
注解的应用
上面创建了一个注解,那么注解的的使用方法是什么呢。
@TestAnnotation public class Test{
}
创建一个类 Test, 然后在类定义的地方加上 @TestAnnotation 就可以用 Test 注解这个类了。
不过,要想注解能够正常工作,还需要介绍一下一个新的概念那就是元注解。
元注解
元注解是可以注解到注解上的注解,或者说元注解是一种基本注解,但是它能够应用到其它的注解上面。
元注解标签有 @Retention、@Documented、@Target、@Inherited、@Repeatable 5 种。
@Retention
Retention 的英文意为保留期的意思。当 @Retention 应用到一个注解上的时候,它解释说明了这个注解的的存活时间。
它的取值如下:
RetentionPolicy.SOURCE 注解只在源码阶段保留,在编译器进行编译时它将被丢弃忽视。
RetentionPolicy.CLASS 注解只被保留到编译进行的时候,它并不会被加载到 JVM 中。注解默认使用这种方式。
RetentionPolicy.RUNTIME 注解可以保留到程序运行的时候,它会被加载进入到 JVM 中,所以在程序运行时可以获取到它们。因此可以使用反射机制读取该注解的信息。
@Retention(RetentionPolicy.RUNTIME) public @interface TestAnnotation {String value() </span><span style="color: rgba(0, 0, 255, 1)">default</span> ""<span style="color: rgba(0, 0, 0, 1)">;
}
@Documented
顾名思义,这个元注解肯定是和文档有关。它的作用是能够将注解中的元素包含到 Javadoc 中去。
@Target
Target 是目标的意思,@Target 指定了注解运用的地方。
你可以这样理解,当一个注解被 @Target 注解时,这个注解就被限定了运用的场景。
@Target 有下面的取值
ElementType.ANNOTATION_TYPE 可以给一个注解进行注解
ElementType.CONSTRUCTOR 可以给构造方法进行注解
ElementType.FIELD 可以给属性进行注解
ElementType.LOCAL_VARIABLE 可以给局部变量进行注解
ElementType.METHOD 可以给方法进行注解
ElementType.PACKAGE 可以给一个包进行注解
ElementType.PARAMETER 可以给一个方法内的参数进行注解
ElementType.TYPE 可以给一个类型进行注解,比如类、接口、枚举
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE)//类、接口(包括注解类型)或枚举 public @interface TestAnnotation {String value() </span><span style="color: rgba(0, 0, 255, 1)">default</span> ""<span style="color: rgba(0, 0, 0, 1)">;
}
@Inherited
Inherited 是继承的意思,但是它并不是说注解本身可以继承,而是说如果一个超类被 @Inherited 注解过的注解进行注解的话,那么如果它的子类没有被任何注解应用的话,那么这个子类就继承了超类的注解。
@Repeatable
Repeatable 自然是可重复的意思。@Repeatable 是 Java 1.8 才加进来的,所以算是一个新的特性。
什么样的注解会多次应用呢?通常是注解的值可以同时取多个。
注解的属性
注解的属性也叫做成员变量。注解只有成员变量,没有方法。注解的成员变量在注解的定义中以“无形参的方法”形式来声明,其方法名定义了该成员变量的名字,其返回值定义了该成员变量的类型。
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface TestAnnotation{</span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> id(); String msg();
}
上面代码定义了 @TestAnnotation 这个注解中拥有 id 和 msg 两个属性。在使用的时候,我们应该给它们进行赋值。
赋值的方式是在注解的括号内以 value="" 形式,多个属性之前用 ,隔开。
@TestAnnotation(id=3,msg="hello annotation") public class Test {}
需要注意的是,使用 @interface 自定义注解时,自动继承了 java.lang.annotation.Annotation 接口,由编译程序自动完成其他细节。在定义注解时,不能继承其他的注解或接口。@interface 用来声明一个注
解,其中的每一个方法实际上是声明了一个属性。方法的名称就是属性的名称,返回值类型就是参数的类型(返回值类型只能是 8 种基本数据类型、Class、String、enum 及它们的数组)。
定义注解格式:
public @interface 注解名 {定义体}
注解参数的可支持数据类型:
1. 所有基本数据类型(int,float,boolean,byte,double,char,long,short)
2.String 类型
3.Class 类型
4.enum 类型
5.Annotation 类型
6. 以上所有类型的数组
Annotation 类型里面的参数该怎么设定:
第一, 只能用 public 或默认 (default) 这两个访问权修饰. 例如,String value(); 这里把方法设为 defaul 默认类型;
第二, 参数成员只能用基本类型 byte,short,char,int,long,float,double,boolean 八种基本数据类型和 String,Enum,Class,annotations 等数据类型, 以及这一些类型的数组. 例如,String value(); 这里的参数成员就为 String;
第三, 如果只有一个参数成员, 最好把参数名称设为 "value", 后加小括号。
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @interface Todo { public enum Priority {LOW, MEDIUM, HIGH} public enum Status {STARTED, NOT_STARTED} String author() default "Yash"; Priority priority() default Priority.LOW; Status status() default Status.NOT_STARTED; }
下面的例子演示了如何使用上面的注解。
@Todo(priority = Todo.Priority.MEDIUM, author = "Yashwant", status = Todo.Status.STARTED) public void incompleteMethod1() { //Some business logic is written //But it’s not complete yet }
注解中属性可以有默认值,默认值需要用 default 关键值指定。
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface TestAnnotation{</span><span style="color: rgba(0, 0, 255, 1)">int</span> id() <span style="color: rgba(0, 0, 255, 1)">default</span> -1<span style="color: rgba(0, 0, 0, 1)">; String msg() defaul </span>"hi"<span style="color: rgba(0, 0, 0, 1)">t;
}
因为有默认值,所以无需要再在 @TestAnnotation 后面的括号里面进行赋值了,这一步可以省略。
@TestAnnotation() public class Test {}
另外,还有一种情况。如果一个注解内仅仅只有一个名字为 value 的属性时,应用这个注解时可以直接接属性值填写到括号内。
java 预置的注解
学习了上面相关的知识,我们已经可以自己定义一个注解了。其实 Java 语言本身已经提供了几个现成的注解。
@Deprecated
这个元素是用来标记过时的元素,想必大家在日常开发中经常碰到。编译器在编译阶段遇到这个注解时会发出提醒警告,告诉开发者正在调用一个过时的元素比如过时的方法、过时的类、过时的成员变量。
@Override
这个大家应该很熟悉了,提示子类要复写父类中被 @Override 修饰的方法
@SuppressWarnings
阻止警告的意思。之前说过调用被 @Deprecated 注解的方法后,编译器会警告提醒,而有时候开发者会忽略这种警告,他们可以在调用的地方通过 @SuppressWarnings 达到目的。
@SafeVarargs
参数安全类型注解。它的目的是提醒开发者不要用参数做一些不安全的操作, 它的存在会阻止编译器产生 unchecked 这样的警告。它是在 Java 1.7 的版本中加入的。
@FunctionalInterface
函数式接口注解,这个是 Java 1.8 版本引入的新特性。函数式编程很火,所以 Java 8 也及时添加了这个特性。
函数式接口 (Functional Interface) 就是一个具有一个方法的普通接口。
我们进行线程开发中常用的 Runnable 就是一个典型的函数式接口,源码可以看到它就被 @FunctionalInterface 注解。
注解的提取
博文前面的部分讲了注解的基本语法,现在是时候检测我们所学的内容了。
要想正确检阅注解的内容信息,离不开一个手段,那就是反射。
注解与反射。
注解通过反射获取。首先可以通过 Class 对象的 isAnnotationPresent() 方法判断它是否应用了某个注解
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass){}
然后通过 getAnnotation() 方法来获取 Annotation 对象。
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {}
或者是 getAnnotations() 方法。
public Annotation[] getAnnotations() {}
前一种方法返回指定类型的注解,后一种方法返回注解到这个元素上的所有注解。
如果获取到的 Annotation 如果不为 null,则就可以调用它们的属性方法了。比如
@TestAnnotation() public class Test {</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { </span><span style="color: rgba(0, 0, 255, 1)">boolean</span> hasAnnotation = Test.<span style="color: rgba(0, 0, 255, 1)">class</span>.isAnnotationPresent(TestAnnotation.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> ( hasAnnotation ) { TestAnnotation testAnnotation </span>= Test.<span style="color: rgba(0, 0, 255, 1)">class</span>.getAnnotation(TestAnnotation.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"id:"+<span style="color: rgba(0, 0, 0, 1)">testAnnotation.id()); System.out.println(</span>"msg:"+<span style="color: rgba(0, 0, 0, 1)">testAnnotation.msg()); } }
}
程序的运行结果是:
id:-1
msg:
这个正是 TestAnnotation 中 id 和 msg 的默认值。
上面的例子中,只是检阅出了注解在类上的注解,其实属性、方法上的注解照样是可以的。同样还是要假手于反射。
@TestAnnotation(msg="hello") public class Test {@Check(value</span>="hi"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> a; @Perform </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testMethod(){} @SuppressWarnings(</span>"deprecation"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> test1(){ Hero hero </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Hero(); hero.say(); hero.speak(); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { </span><span style="color: rgba(0, 0, 255, 1)">boolean</span> hasAnnotation = Test.<span style="color: rgba(0, 0, 255, 1)">class</span>.isAnnotationPresent(TestAnnotation.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> ( hasAnnotation ) { TestAnnotation testAnnotation </span>= Test.<span style="color: rgba(0, 0, 255, 1)">class</span>.getAnnotation(TestAnnotation.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取类的注解</span> System.out.println("id:"+<span style="color: rgba(0, 0, 0, 1)">testAnnotation.id()); System.out.println(</span>"msg:"+<span style="color: rgba(0, 0, 0, 1)">testAnnotation.msg()); } </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { Field a </span>= Test.<span style="color: rgba(0, 0, 255, 1)">class</span>.getDeclaredField("a"<span style="color: rgba(0, 0, 0, 1)">); a.setAccessible(</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取一个成员变量上的注解</span> Check check = a.getAnnotation(Check.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span> ( check != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)"> ) { System.out.println(</span>"check value:"+<span style="color: rgba(0, 0, 0, 1)">check.value()); } Method testMethod </span>= Test.<span style="color: rgba(0, 0, 255, 1)">class</span>.getDeclaredMethod("testMethod"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span> ( testMethod != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)"> ) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取方法中的注解</span> Annotation[] ans =<span style="color: rgba(0, 0, 0, 1)"> testMethod.getAnnotations(); </span><span style="color: rgba(0, 0, 255, 1)">for</span>( <span style="color: rgba(0, 0, 255, 1)">int</span> i = 0;i < ans.length;i++<span style="color: rgba(0, 0, 0, 1)">) { System.out.println(</span>"method testMethod annotation:"+<span style="color: rgba(0, 0, 0, 1)">ans[i].annotationType().getSimpleName()); } } } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (NoSuchFieldException e) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated catch block</span>
e.printStackTrace();
System.out.println(e.getMessage());
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
}}
}
它们的结果如下:
id:-1
msg:hello
check value:hi
method testMethod annotation:Perform
需要注意的是,如果一个注解要在运行时被成功提取,那么 @Retention(RetentionPolicy.RUNTIME) 是必须的。
注解的使用场景
Java 官方文档写明:
注解是一系列元数据,它提供数据用来解释程序代码,但是注解并非是所解释的代码本身的一部分。注解对于代码的运行效果没有直接影响。
注解有许多用处,主要如下:
提供信息给编译器: 编译器可以利用注解来探测错误和警告信息
编译阶段时的处理: 软件工具可以用来利用注解信息来生成代码、Html 文档或者做其它相应处理。
运行时的处理: 某些注解可以在程序运行的时候接受代码的提取
值得注意的是,注解不是代码本身的一部分。
注解同样无法改变代码本身,注解只是某些工具的的工具。
还是回到官方文档的解释上,注解主要针对的是编译器和其它工具软件 (SoftWare tool)。
当开发者使用了 Annotation 修饰了类、方法、Field 等成员之后,这些 Annotation 不会自己生效,必须由开发者提供相应的代码来提取并处理 Annotation 信息。这些处理提取和处理 Annotation 的代码统称为 APT(Annotation Processing Tool)。
现在,我们可以给自己答案了,注解有什么用?给谁用?给 编译器或者 APT 用的。
亲手自定义注解完成某个目的
我要写一个测试框架,测试程序员的代码有无明显的异常。
—— 程序员 A : 我写了一个类,它的名字叫做 NoBug,因为它所有的方法都没有错误。
—— 我:自信是好事,不过为了防止意外,让我测试一下如何?
—— 程序员 A: 怎么测试?
—— 我:把你写的代码的方法都加上 @Jiecha 这个注解就好了。
—— 程序员 A: 好的。
package ceshi; import ceshi.Jiecha;public class NoBug {
@Jiecha </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> suanShu(){ System.out.println(</span>"1234567890"<span style="color: rgba(0, 0, 0, 1)">); } @Jiecha </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> jiafa(){ System.out.println(</span>"1+1="+1+1<span style="color: rgba(0, 0, 0, 1)">); } @Jiecha </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> jiefa(){ System.out.println(</span>"1-1="+(1-1<span style="color: rgba(0, 0, 0, 1)">)); } @Jiecha </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> chengfa(){ System.out.println(</span>"3 x 5="+ 3*5<span style="color: rgba(0, 0, 0, 1)">); } @Jiecha </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> chufa(){ System.out.println(</span>"6 / 0="+ 6 / 0<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> ziwojieshao(){ System.out.println(</span>"我写的程序没有 bug!"<span style="color: rgba(0, 0, 0, 1)">); }
}
package ceshi;import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;@Retention(RetentionPolicy.RUNTIME)
public @interface Jiecha {}
然后,我再编写一个测试类 TestTool 就可以测试 NoBug 相应的方法了。
package ceshi;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;public class TestTool {
NoBug testobj = new NoBug();</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated method stub</span>
Class clazz </span>=<span style="color: rgba(0, 0, 0, 1)"> testobj.getClass(); Method[] method </span>=<span style="color: rgba(0, 0, 0, 1)"> clazz.getDeclaredMethods(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">用来记录测试产生的 log 信息</span> StringBuilder log = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringBuilder(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 记录异常的次数</span> <span style="color: rgba(0, 0, 255, 1)">int</span> errornum = 0<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> ( Method m: method ) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 只有被 @Jiecha 标注过的方法才进行测试</span> <span style="color: rgba(0, 0, 255, 1)">if</span> ( m.isAnnotationPresent( Jiecha.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> )) { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { m.setAccessible(</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">); m.invoke(testobj, </span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated catch block </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">e.printStackTrace();</span> errornum++<span style="color: rgba(0, 0, 0, 1)">; log.append(m.getName()); log.append(</span>" "<span style="color: rgba(0, 0, 0, 1)">); log.append(</span>"has error:"<span style="color: rgba(0, 0, 0, 1)">); log.append(</span>"\n\r caused by "<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">记录测试过程中,发生的异常的名称</span>
log.append(e.getCause().getClass().getSimpleName());
log.append("\n\r");
//记录测试过程中,发生的异常的具体信息
log.append(e.getCause().getMessage());
log.append("\n\r");
}
}
}log.append(clazz.getSimpleName()); log.append(</span>" has "<span style="color: rgba(0, 0, 0, 1)">); log.append(errornum); log.append(</span>" error."<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 生成测试报告</span>
System.out.println(log.toString());
}
}
测试的结果是:
1234567890
1+1=11
1-1=0
3 x 5=15
chufa has error:
caused by ArithmeticException
/ by zero
NoBug has 1 error.
提示 NoBug 类中的 chufa() 这个方法有异常,这个异常名称叫做 ArithmeticException,原因是运算过程中进行了除 0 的操作。
所以,NoBug 这个类有 Bug。
这样,通过注解我完成了我自己的目的,那就是对别人的代码进行测试。
注解应用实例
注解的功能很强大,Spring 和 Hebernate 这些框架在日志和有效性中大量使用了注解功能。注解可以应用在使用标记接口的地方。不同的是标记接口用来定义完整的类,但你可以为单个的方法定义注释,例如是否将一个方法暴露为服务。
在最新的 servlet3.0 中引入了很多新的注解,尤其是和 servlet 安全相关的注解。
HandlesTypes –该注解用来表示一组传递给 ServletContainerInitializer 的应用类。
HttpConstraint – 该注解代表所有 HTTP 方法的应用请求的安全约束,和 ServletSecurity 注释中定义的 HttpMethodConstraint 安全约束不同。
HttpMethodConstraint – 指明不同类型请求的安全约束,和 ServletSecurity 注解中描述 HTTP 协议方法类型的注释不同。
MultipartConfig –该注解标注在 Servlet 上面,表示该 Servlet 希望处理的请求的 MIME 类型是 multipart/form-data。
ServletSecurity 该注解标注在 Servlet 继承类上面,强制该 HTTP 协议请求遵循安全约束。
WebFilter – 该注解用来声明一个 Server 过滤器;
WebInitParam – 该注解用来声明 Servlet 或是过滤器的中的初始化参数,通常配合 @WebServlet 或者 @WebFilter 使用。
WebListener –该注解为 Web 应用程序上下文中不同类型的事件声明监听器。
WebServlet –该注解用来声明一个 Servlet 的配置。
总结:
- 如果注解难于理解,你就把它类同于标签,标签为了解释事物,注解为了解释代码。
- 注解的基本语法,创建如同接口,但是多了个 @ 符号。
- 注解的元注解。
- 注解的属性。
- 注解主要给编译器及工具类型的软件用的。
- 注解的提取需要借助于 Java 的反射技术,反射比较慢,所以注解使用时也需要谨慎计较时间成本。