注解@Retention
what?
- 这个在java中处于什么样的存在?它在java用于java文件编译成class文件时才会使用到的注解,用于判断是否在java编译时保存该修饰的文件
why?
- 在java中编写代码会出现三个周期,Java源文件(.java文件) —> .class文件 —> 内存中的字节码。在java编写代码中灵活在三个周期使用(个人想法)
how?
- RetentionPolicy.SOURCE:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;
- RetentionPolicy.CLASS:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期;
- RetentionPolicy.RUNTIME:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在;
一个具有注脚的文本。
一般情况下只要用到RetentionPolicy.RUNTIME即可,那怎么来选择合适的注解生命周期呢?(个人不建议新手使用在项目中)1
注解@Target
what?
- java中这个注解是用于修饰注解2,用于说明该修饰的注解定义的方法是作用于什么类型上
why?
- Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。
how?
取值(ElementType)有:
- CONSTRUCTOR:用于描述构造器
- FIELD:用于描述域
- LOCAL_VARIABLE:用于描述局部变量
- METHOD:用于描述方法
- PACKAGE:用于描述包
- PARAMETER:用于描述参数
- TYPE:用于描述类、接口(包括注解类型) 或enum声明
@Target(ElementType.TYPE) //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包
注解@Inherited
@Inherited 注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。
@Documented注解
@Documented用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。Documented也是一个标记注解,没有成员。
自定义注解
ElementType.TYPE注解的使用
import com.example.demo.common.MsgType;
import java.lang.annotation.*;
@Documented
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MsgTypeHandler {
MsgType value();
}
import com.example.demo.common.MsgType;
import com.example.demo.context.annotation.MsgTypeHandler;
import com.example.demo.model.MessageInfo;
import com.example.demo.service.MessageService;
import org.springframework.stereotype.Service;
/**
* @program:demo
* @description:用于处理图片类型
* @author: Mr.Henry
* @create:2020/2/10 15:29
*/
@Service
@MsgTypeHandler(MsgType.IMAGE)
public class MessaeImageService implements MessageService {
@Override
public void handleMessage(MessageInfo messageInfo) {
System.out.println("处理图片消息:"+messageInfo.getContext());
}
}
ElementType.FIELD使用
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 汽车名称注解
* @author Mr.Henry
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CarName {
String value() default "";
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 水果颜色注解
* @author Mr.Henry
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CarColor {
/**
* 颜色枚举
* @author peida
*
*/
public enum Color{ BULE,RED,GREEN};
/**
* 颜色属性
* @return
*/
Color fruitColor() default Color.GREEN;
import com.example.demo.annotation.CarColor.Color;
@Data
@AllArgsConstructor
public class Car{
@CarName("劳斯莱斯")
private String carName;
@FruitColor(fruitColor=Color.RED)
private String appleColor;
}