视频链接:https://www.bilibili.com/video/BV1Rx411876f?p=1
视频范围P834 - P44
目录描述
1.注解
- 注解,或者叫做注释,英文单词为:Annotation
- 注解Annotation是一种引用数据类型,编译之后也是生成xxx.class文件
- 语法格式
[修饰符列表] @interface 注解类型名{
}
- 注解使用时的语法格式:@注解类型名
- 注解可以出现在类上、属性上、方法上、变量上等…注解还可以出现在注解类型上
- 默认情况下,注解可以出现在任意位置
举例:
package annotation;
/*
自定义注解:MyAnnotation
*/
public @interface MyAnnotation {
}
package annotation;
//默认情况下,注解可以出现在任意位置
@MyAnnotation
public class AnnotationTest01 {
@MyAnnotation
private int no;
@MyAnnotation
public AnnotationTest01(){};
@MyAnnotation
public static void m1(){
@MyAnnotation
int i = 100;
}
@MyAnnotation
public void m2(@MyAnnotation String name,@MyAnnotation int k){};
}
@MyAnnotation
interface MyInterface{
}
@MyAnnotation
enum Seanson{
SPRING,SUMMER,AUTUMN,WINTER
}
package annotation;
//注解修饰注解
@MyAnnotation
public @interface OtherAnnotation {
}
2.JDK内置注解
java.lang包下的注释类型:
注释 | 备注 |
---|---|
Deprecated | 注释@Deprecated的程序元素是程序员不鼓励使用的程序元素,通常是因为它是危险的,或者因为存在更好的替代方法 |
Override | 表示方法声明旨在覆盖超类型中的方法声明 |
SuppressWarnings | 表示在注释元素(以及注释元素中包含的所有程序元素)中应该抑制命名的编译器警告 |
2.1 Override注解
- @Override这个注解只能注解方法
- 源代码
public @interface Override {
}
- @Override这个注解是给编译器参考的,和运行阶段没有关系
- 凡是java中的方法带有@Override注解的,编译器都会进行编译检查,如果这个方法不是重写父类的方法,编译器报错
package annotation;
public class AnnotationTest02 {
@Override
public String toString(){
return "toString";
}
}
2.2 元注解
元注解:用来标注“注解类型”的“注解”
常见的元注解:Target,Retention
- Target
用来标注“被标注的注解”可以出现在哪些位置上
@Target(ElementType.METHOD) :表示“被标注的注解”只能出现在方法上
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, MODULE, PARAMETER, TYPE})
表示该注解可以出现在:构造方法上,字段上,局部变量上,方法上,类上… - Retention
用来标注“被标注的注解”最终保存在哪里
@Retention(RetentionPolicy.SOURCE):表示该注解只被保留在java源文件中
@Retention(RetentionPolicy.CLASS):表示该注解被保留在class文件中
@Retention(RetentionPolicy.RUNTIME):表示该注解被保留在class文件中,并且可以被反射机制所读取到
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
Retention源代码:
//元注解
public @interface Retention {
//属性
RetentionPolicy value();
}
RetentionPolicy源代码:
public enum RetentionPolicy {
SOURCE,
CLASS,
RUNTIME
}
例子:
@Retention(value = RetentionPolicy.SOURCE)//这个注解只保留在源代码中
//等价于@Retention(RetentionPolicy.SOURCE)
public @interface MyAnnotation {
}
2.3 Deprecated注解
package annotation;
public class AnnotationTest03 {
public static void main(String[] args) {
}
@Deprecated
public void doSome(){
System.out.println("do something!");
}
//Deprecated这个注解标注的元素已过时
//这个注解主要是向其它程序员传达一个信息,告知已过时,有更好的解决方案存在
@Deprecated
public static void doOther(){
System.out.println("do other...");
}
}
class T{
public static void main(String[] args) {
AnnotationTest03 at = new AnnotationTest03();
at.doSome();
AnnotationTest03.doOther();
try {
Class c = Class.forName("java.util.Date");
Object obj = c.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行效果:
3.注解中定义属性
3.1 概念
注解:
package annotation2;
public @interface MyAnnotation {
/**
* 通常在注解当中定义属性,以下是这个是MyAnnotation的name属性
* 看着像1个方法,但实际上称之为属性name
* @return
*/
String name();
//颜色属性
String color();
//年龄属性
int age() default 25;//属性默认值
}
测试类:
package annotation2;
public class MyAnnotationTest {
//如果一个注解当中有属性,那么必须给属性赋值(除非该属性使用default指定了默认值)
//@MyAnnotation(属性名=属性值)
@MyAnnotation(name = "zhangsan",color = "hongse")
public void doSome(){
}
}
3.2 属性为value
如果一个注解的属性的名字是value,并且只有一个属性的话,在使用的时候,该属性名可以省略
package annotation3;
public @interface MyAnnotation {
//指定一个value属性
String value();
//String email();
}
package annotation3;
public class MyAnnotationTest {
@MyAnnotation(value = "hehe")
public void doSome(){
}
@MyAnnotation("haha")
public void doOther(){
}
}
3.3 属性是一个数组
属性的类型可以是:byte short int long float double boolean char String Class 枚举类型,以及以上每一种的数组形式
package annotation4;
public @interface MyAnnotation {
int value1();
String value2();
int[] value3();
String[] value4();
Season value5();
Season[] value6();
Class paramerterType();
Class[] paramerterTypes();
}
枚举:
package annotation4;
public enum Season {
SPRING,SUMMER,AUTUMN,WINTER
}
注解:
package annotation4;
import java.lang.annotation.Retention;
public @interface OtherAnnotation {
//年龄属性
int age();
//邮箱地址属性,支持多个
String[] email();
/**
* 季节数组
* @return
*/
Season[] seasonArray();
}
测试类:
package annotation4;
public class OtherAnnotationTest {
//数组是大括号
@OtherAnnotation(age = 25,email = {"zhangsan@123.com","zhangsan@sohu.com"},seasonArray = Season.WINTER)
public void doSome(){
}
//如果数组中只有1个元素:大括号可以省略
@OtherAnnotation(age = 25,email = "zhangsan@123.com",seasonArray = {Season.SPRING,Season.SUMMER})
public void doOther(){
}
}
4.反射注解
注解:
package annotation5;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//只允许该注解可以标注类,方法
@Target({ElementType.TYPE,ElementType.METHOD})
//希望这个注解可以被反射
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
//value属性
String value() default "重庆北碚";
}
MyAnnotationTest类:
package annotation5;
@MyAnnotation
public class MyAnnotationTest {
@MyAnnotation
public void doSome(){
}
}
反射类:
package annotation5;
public class ReflectAnnotationTest {
public static void main(String[] args) throws Exception {
//获取类
Class c = Class.forName("annotation5.MyAnnotationTest");
//判断类上面是否有@MyAnnotation
System.out.println(c.isAnnotationPresent(MyAnnotation.class));//输出为:true
if (c.isAnnotationPresent(MyAnnotation.class)){
//获取该注解对象
MyAnnotation myAnnotation = (MyAnnotation) c.getAnnotation(MyAnnotation.class);
System.out.println("类上面的注解对象:" + myAnnotation);
//获取注解对象的属性,和调接口没有区别
String value = myAnnotation.value();
System.out.println(value);
}
//判断String类上面是否存在这个注解
Class stringClass = Class.forName("java.lang.String");
System.out.println(stringClass.isAnnotationPresent(MyAnnotation.class));//输出为:false
}
}
运行结果:
5.通过反射获取注解对象属性的值
注解:
package annotation6;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
//username属性
String username();
//password属性
String password();
}
测试类:
package annotation6;
import java.lang.reflect.Method;
public class MyAnnotationTest {
@MyAnnotation(username = "admin",password = "123")
public void doSome(){
}
public static void main(String[] args) throws Exception {
//获取MyAnnotationTest的doSome()方法上面的注解信息
Class c = Class.forName("annotation6.MyAnnotationTest");
//获取doSome()方法
Method doSomeMethod = c.getDeclaredMethod("doSome");
//判断该方法上是否存在这个注解
if (doSomeMethod.isAnnotationPresent(MyAnnotation.class)){
MyAnnotation myAnnotation = doSomeMethod.getAnnotation(MyAnnotation.class);
System.out.println(myAnnotation.username());
System.out.println(myAnnotation.password());
}
}
}
运行结果:
6.注解在开发中的使用
需求:
假设有这释一个注解,叫做:@Id
1.这个注解只能出现在类上面
2.当这个类上有这个注解的时候,要求这个类中必须有一个int类型的id属性
3.如果没有这个属性就报异常,
如果有这个属性则正常执行
4.这个注解@Id用来标注类,被标注的类中必须有一个int类型的id属性,没有就报异常
Id注释:
package annotation7;
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 Id {
}
User类:
package annotation7;
@Id
public class User {
int id;
String name;
String passward;
}
错误类:
package annotation7;
public class HasNotIdPropertyException extends RuntimeException{
public HasNotIdPropertyException() {
}
public HasNotIdPropertyException(String s) {
super(s);
}
}
测试类:
package annotation7;
import java.lang.reflect.Field;
public class Test {
public static void main(String[] args) throws Exception {
//获取类
Class userClass = Class.forName("annotation7.User");
//判断类上是否存在Id注解
if (userClass.isAnnotationPresent(Id.class)){
//当一个类上面有@Id注解的时候,要求类中必须存在int类型的id属性
//如果没有int类型的id属性则报异常
//获取类的属性
Field[] fields = userClass.getDeclaredFields();
boolean isOk = false;//给一个默认的标记
for (Field field :fields){
if ("id".equals(field.getName()) && "int".equals(field.getType().getSimpleName())){
//表示这个类是合法的类,有@Id注解,则这个类中必须有int类型的id
isOk = true;//表示合法
break;
}
}
//判断是否合法
if (!isOk){
throw new HasNotIdPropertyException("被@Id注解标注的类中必须要有一个int类型的id属性!");
}
}
}
}