package com.annotation;
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.TYPE)
public @interface ItcastAnnotation {
String color() default "red";
String value();
}
package com.annotation;
/*
* 定义注解(注解的作用范围,注解target) 使用注解 使用 有注解的类的类
*/
@ItcastAnnotation("wangweijun")
public class AnnotationTest {
public static void main(String[] args) {
if (AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)) {
ItcastAnnotation annotation = AnnotationTest.class.getAnnotation(ItcastAnnotation.class);
System.out.println(annotation.color());
System.out.println(annotation.value());
}
}
}
以前只知道使用注解,像spring框架里头有太多的注解,看过张老师的视频之后,让我对注解有一个更深刻的理解,其实注解就是一个特殊的类,spring 的DI注入bean,就是一个最好的例子