Java中内置注解、元注解、自定义注解

一: 内置注解:

  1. @Override 重写的注解,十分常见

  2. @Deprecated 废弃类,被标注的元素一般是不推荐程序员使用的内容,当然,也可能是说比起该方法,java存在其他更优秀的选择

    常见的方法有Date,如果用Date实例出的对象去调用其相关方法的话就会看到方法中间存在斜线,点进去查看源码的时候就能发现上方存在@Deprecated注解,当然,不推荐使用不代表不可以使用
    在这里插入图片描述

    源码:

    在这里插入图片描述


  3. @SuppressWarnings 镇压警告,定义在java.lang.SuppressWarnings类中,可以抑制编译时的报错信息。
    比如,直接在java中定义了一个没被使用的变量,编译器会报错,提示你变量未被使用,在Eclipse中会出现左侧警示符,变量下出现淡黄波浪线,IDEA中则变量会变色,该注解支持作用于在类。
    在这里插入图片描述

    加注解,当然注解需要一个参数,选择镇压全部即不再出现警告提示

    在这里插入图片描述





二:元注解

元注解负责注解其他注解

  1. @Target 用于描述注解的使用范围
    参考Override的注解源码,可以看到其范围为METHOD,说明作用在方法上,因此我们不会把Override加在整个类上。在这里插入图片描述
    而像@SuppressWarnings注解,其作用范围就相对广泛了。
    在这里插入图片描述
    至于@Target中传入的参数,我们打开其源码。可以看到@Target中传入的是一个ElementType
    在这里插入图片描述
    接着进入ElementType源码,参数的详细信息已经在源码中介绍了。
public enum ElementType {
    /** Class, interface (including annotation type), or enum declaration */
    TYPE,

    /** Field declaration (includes enum constants) */
    FIELD,

    /** Method declaration */
    METHOD,

    /** Formal parameter declaration */
    PARAMETER,

    /** Constructor declaration */
    CONSTRUCTOR,

    /** Local variable declaration */
    LOCAL_VARIABLE,

    /** Annotation type declaration */
    ANNOTATION_TYPE,

    /** Package declaration */
    PACKAGE,

    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,

    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE
}




  1. @Retention 表示在什么级别保存该注释信息,描述注解生命周期(SOURCE<CLASS<RUNTIME)

    例如@Override源码,其运行级别就是SOURCE源码级,也就是说是给java代码看的,而java程序跑起来想要实现业务逻辑的时候是不会管你是不是override的的,它需要的只是找方法然后能实现功能就行了,也就是运行RUNTIME级别。在这里插入图片描述
    运行级别的列子比如RequestMapping,如果让程序在运行的时候找不到RequestMapping定义的注解,那接口的存在就没意义了
    在这里插入图片描述




  2. @Documented 说明该注解被包含在javadoc中

  3. @Inherited 说明子类可以继承父类中的该注解





三:自定义注解

使用@interface可以自动继承java.lang.annotation.Annotation接口,格式:@interface 注解名 {内容}

简单示例:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class test01 {

	@MyAnnoation
	public void test() {
		
	}
}

//定义一个注解
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnoation{
	
}




有参注解:

注意定义的注解中的内容为参数而不是方法,同时可以使用default方法来为注解指定默认值,如果不指定,那注解中此参数必填,如果存在多参数,填写的参数没有顺序要求。

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class test02 {

	@PersonAnnotation(names = { "admin,admin1" })
	public void test() {
		
	}
}

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface PersonAnnotation{
	
	//这是注解的参数,不是一个方法 default ""表示参数默认值为空,因此在使用注解的时候可以不写name参数
	String name() default "";
	
	String[] names();
}




唯一value参注解:

假设注解中只有一个参数,且参数名固定是value,那即便是value本身没有用default指定默认值,依然可以在使用注解的时候直接指定,而无需重新申明参数。

例如@SuppressWarnings注解源码:

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    /**
     * The set of warnings that are to be suppressed by the compiler in the
     * annotated element.  Duplicate names are permitted.  The second and
     * successive occurrences of a name are ignored.  The presence of
     * unrecognized warning names is <i>not</i> an error: Compilers must
     * ignore any warning names they do not recognize.  They are, however,
     * free to emit a warning if an annotation contains an unrecognized
     * warning name.
     *
     * <p> The string {@code "unchecked"} is used to suppress
     * unchecked warnings. Compiler vendors should document the
     * additional warning names they support in conjunction with this
     * annotation type. They are encouraged to cooperate to ensure
     * that the same names work across multiple compilers.
     * @return the set of warnings to be suppressed
     */
    String[] value();
}

唯一参数value,那如下两种写法都可以:


在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Deeeelete

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值