java常用注解的运用(入门)

注解@Retention

what?

  • 这个在java中处于什么样的存在?它在java用于java文件编译成class文件时才会使用到的注解,用于判断是否在java编译时保存该修饰的文件

why?

  • 在java中编写代码会出现三个周期,Java源文件(.java文件) —> .class文件 —> 内存中的字节码。在java编写代码中灵活在三个周期使用(个人想法)

how?

  1. RetentionPolicy.SOURCE:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;
  2. RetentionPolicy.CLASS:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期;
  3. RetentionPolicy.RUNTIME:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在;
    一个具有注脚的文本。

一般情况下只要用到RetentionPolicy.RUNTIME即可,那怎么来选择合适的注解生命周期呢?(个人不建议新手使用在项目中)1

注解@Target

what?

  1. java中这个注解是用于修饰注解2,用于说明该修饰的注解定义的方法是作用于什么类型上

why?

  1. Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。

how?

取值(ElementType)有:

  1. CONSTRUCTOR:用于描述构造器
  2. FIELD:用于描述域
  3. LOCAL_VARIABLE:用于描述局部变量
  4. METHOD:用于描述方法
  5. PACKAGE:用于描述包
  6. PARAMETER:用于描述参数
  7. 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;
}

  1. 首先要明确生命周期长度 SOURCE < CLASS < RUNTIME ,所以前者能作用的地方后者一定也能作用。一般如果需要在运行时去动态获取注解信息,那只能用 RUNTIME 注解;如果要在编译时进行一些预处理操作,比如生成一些辅助代码(如 ButterKnife),就用 CLASS注解;如果只是做一些检查性的操作,比如 @Override 和 @SuppressWarnings,则可选用 SOURCE 注解。 ↩︎

  2. 这个注解为自己定义的注解内容 ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值