Java注解笔记

一、什么是注解

Java注解又称Java标注,是在 JDK5 时引入的新特性,注解 (也被称为元数据)

Javaa注解它提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metadata) 与程元素

类、方法、成员变量等)进行关联 

二、注解的应用

1.生成文档这是最常见的,也是iava 最早提供的注解

2.在编译时进行格式检查,如@Overide放在方法前,如果你这个方法并不是看盖了 超类Q方法,则编译时就能检查出

3.跟踪代码依赖性,实现替代配置文件功能,比较常见的是spring 2.5 开始的基于注解配置,作用就是减少配置

4.在反射的 Class,Method,Field 等函数中,有许多于 Annotation 相关的接口,可以在反射中解析并使用 Annotation。

三、注解的分类

Java的注解分为三类:1.标准注解、2.元注解、3.自定义注解

1.标准注解

注解名称 功能描述
@Override 检查该方法是否是重写方法,如果发现其父类,或者是引用的接口中并没有该方法时,会报编译错误
@Deprecated 标记过时的方法,如果使用该方法,会报编译警告
@SuppressWarnings 指示编译器去忽略注释解中声明的警告
@Functionalinterface java8支持,标识一个匿名函数或函数式接口

 @Override

class Parent {
    public void test() {
    }    
}


class Child extends Parent {
    /*放开下面的注释,编译时会告警

@override
public void test() {

    }*/
}

@Deprecated 

@Deprecated 用于标明被修饰的类或类成员、类方法已经废弃、过时,不建议使用
@Deprecated
class TestClass{

}

 @SuppressWarnings

//1.抑制单类型的警告、
@Suppresswarnings("unchecked")
    public void additems(String item){
        @Suppresswarnings("rawtypes")
            List items = new ArrayList();
            items.add(item);
}

//2.抑制多类型的警告
@Suppresswarnings(value={"unchecked","rawtypes"})
    public void additems(String item){
        List items = new ArrayList();
        items.add(item);
}


//3.抑制所有类型的警告
@Suppresswarnings("all")
public void additems(String item){
    List items = new ArrayList();
        items.add(item);
}

 @SuppressWarnings常见的参数值: 

参数 作用
deprecation 使用了不赞成使用的类或方法时的警告
unchecked 执行了未检查的转换时的警告,例如当使用集合时没有用泛型
(Generics) 来指定集合保存的类型
fallthrough 当 Switch 程序块直接通往下一种情况而没有 Break 时的警告
path 在类路径、源文件路径等中有不存在的路径时的警告
serial 当在可序列化的类上缺少 serialVersionUID 定义时的警告
finally 任何 finally 子句不能正常完成时的警告
all 所有的警告

 @Functionalinterface

@Functionalinterface
publice interface UserService{
    void getUser(Long userId);

//默认方法,可以用多个默认方法
publice default viod setUser(){

}

//静态方法
publice static void saveUser(){

}
    }

2.元注解

参数 作用
deprecation 使用了不赞成使用的类或方法时的警告
unchecked 执行了未检查的转换时的警告,例如当使用集合时没有用泛型
(Generics) 来指定集合保存的类型
fallthrough 当 Switch 程序块直接通往下一种情况而没有 Break 时的警告
path 在类路径、源文件路径等中有不存在的路径时的警告
serial 当在可序列化的类上缺少 serialVersionUID 定义时的警告
finally 任何 finally 子句不能正常完成时的警告
all 所有的警告

@Retention:用来定义该注解在哪一个级别可用,在源代码中(SOURCE)、类文件中(CLASS)或者运行时(RUNTIME)。

@Documented@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE
public @interface Retention {
RetentionPolicy value();
}

public enum RetentionPolicy {
    SOURCE;

    CLASS;

    RUNTIME;
}

@Documented :生成文档信息的时候保留注解,对类作辅助说明

@Target(ElementType. FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Column {
    public string name() default "fieldName";
    public string setFuncName() default "setField";
    public string getFuncName() default "getField"
    public boolean defaultDBValue() default false;

@Target:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType .ANNOTATION_TYPE)
public @interface Target {
    ElementType[] value();
}

@Repeatable:表示注解可以重复使用。当我们需要重复使用某个注解时,希望利用相同的注解来表现所有的形式时,我们可以借助@Repeatable注解

@Target((ElementType .METHOD,ElementType.ANNOTATION TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Schedules f
scheduled[] value();
}

@Target({ElementType.METHOD,ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(schedules.class)
public @interface scheduled{
}

你可能感兴趣的:(java,开发语言)