网站开发前端和后端,响应式网站建设公司',深圳开发网站建设哪家好,wordpress付费插件网站一、什么是注解#xff1f;
注解是元数据#xff0c;所谓元数据就是描述数据的数据。
在annotation诞生之前#xff08;jdk1.5诞生了注解#xff09;#xff0c;甚至之后#xff0c;xml被广泛的由于描述元数据。但是后来#xff0c;有一些应用开发工程师和架构师觉得它…一、什么是注解
注解是元数据所谓元数据就是描述数据的数据。
在annotation诞生之前jdk1.5诞生了注解甚至之后xml被广泛的由于描述元数据。但是后来有一些应用开发工程师和架构师觉得它越来越难以维护了他们觉得需要一种和代码紧耦合的东西而不是xml配置和代码是松耦合的某些情况下甚至是完全分离的于是annotation诞生了。
假如你想为应用设置很多的常量或参数这种情况下XML是一个很好的选择因为它不会同特定的代码相连。如果你想把某个方法声明为服务那么使用Annotation会更好一些因为这种情况下需要注解和方法紧密耦合起来开发人员也必须认识到这点。
目前许多框架将XML和Annotation两种方式结合使用平衡两者之间的利弊。
二、使用注解的好处
对代码进行标记生成文档编译时进行语法检查减少XML配置
三、注解的分类
3.1、元注解
用来定义注解的注解interface、Target、Retention、Inherited
注解作用interface定义注解Target声明注解的使用目标说明这个可以在类型上使用还是属性、还是方法、还是参数Retention注解应该如何去保留是在源码中保留还是在字节码文件中保留还是在运行时保留Inherited子类是否继承父类在类上定义的注解
3.2、内置注解
jdk定义的一些注解还有很多其他的这里只列举一部分SuppressWarnings、Deprecated、Override、Documented、FunctionaInterface、SafeVarargs、version、author、param、exception、return
SuppressWarnings忽略警告Deprecated弃用Override说明此方法重写父类的方法Documented生成javadoc生成javadoc时默认是不生成注解说明的但是如果使用了Documented来定义这个注解那么将会生成注解说明
3.3、自定义注解
使用interface来自定义一个注解
四、注解使用示例
我们先来定义几个注解
4.1、注解定义
package org.cc.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;Target(ElementType.TYPE)
Retention(RetentionPolicy.RUNTIME)
public interface Controller {String value();
}package org.cc.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;Inherited
Target(ElementType.FIELD)
Retention(RetentionPolicy.RUNTIME)
public interface Inject {String value();
}package org.cc.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;Inherited
Target({ElementType.PARAMETER})
Retention(RetentionPolicy.RUNTIME)
public interface Parameter {String value() default ;
}package org.cc.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;Inherited
Target({ElementType.FIELD,ElementType.METHOD})
Retention(RetentionPolicy.RUNTIME)
public interface Permission {String value() default root;
}package org.cc.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;Target({ElementType.PARAMETER})
Retention(RetentionPolicy.RUNTIME)
public interface Required {
}4.2、使用注解
package org.cc.annotation.use;import org.cc.annotation.Controller;
import org.cc.annotation.Inject;
import org.cc.annotation.Parameter;
import org.cc.annotation.Permission;
import org.cc.annotation.Required;
/*** 定义一个类来使用刚刚定义的注解* author cc**/
Controller(testController)
public class TestController {Inject(injectStr)private String injectStr;Permission(testPermission)public void test(Parameter(param) Required String param,String param2){}
}package org.cc.annotation.use;/*** TestController类的子类* author cc**/
public class SubTestController extends TestController{private String injectStr;public void test(String param,String param2){}
}4.3、解析注解
package org.cc.annotation.use;import java.lang.annotation.Annotation;
import java.lang.reflect.Field;import org.cc.annotation.Controller;
import org.cc.annotation.Inject;/*** 使用反射来解析注解* author cc**/
public class JXAnnotation {public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException, NoSuchMethodException {Class clazz Class.forName(org.cc.annotation.use.TestController);Annotation[] annotations clazz.getAnnotations();//得到指定类的所有注解for (Annotation a: annotations ) {System.out.println(a);}Controller c (Controller)clazz.getAnnotation(Controller.class);//得到类特定的注解System.out.println(c.value());Field f clazz.getDeclaredField(injectStr);Inject inject f.getAnnotation(Inject.class);System.out.println(inject.value());Class clazz1 Class.forName(org.cc.annotation.use.SubTestController);Controller c1 (Controller)clazz1.getAnnotation(Controller.class);//得到类特定的注解System.out.println(c1);//因为定义Controller时没有使用Inherited元注解所以子类SubTestController并不会继承父类的注解Controller//注意仅仅针对类成员属性和方法不受此注释影响就是说子类方法上的注解一定会继承父类的注解}}五、定义和使用注解需要注意的地方
1、自定义注解我们可以看到有一行String value() 类似于定义一个方法这行代码表示使用注解时必须指定value属性值像这样Controller(testController)或者这样写Controller(valuetestController)不能这样写Controller()编译会报错。如果非要这样使用那么需要修改这个注解的定义改成这样String value() default ;或者直接将String value()这一行删掉。
简单描述就是如果定义注解时定义了value属性那么使用时必须设置value属性值或者提供默认的属性值。
2、定义注解时属性是什么类型那么使用注解时设置的属性值必须跟定义的类型相同。下面这种情况也是可以的定义时属性值类型为字符串数组然后使用时设置属性值为字符串。