spring中的java基础(注解)

spring中的java基础(注解)

注解

一、什么是注解

它提供了一种安全的类似注释的机制,用来将任何 的信息或元数据与程序元素进行关联。为程序的元素加上更直关、更明了的说明,这些说明信息与业务逻辑无关,并且供制定的工具或框架使用。

类似@Overrude这种其实就是注解中一种。

二、内置注解

1、@Deprecated

编译器在编译阶段遇到这个注解时会发出提醒警告,告诉开发者正在调用一个过时的元素,比如过时的方法:过时的类、过时的成员变量。

2、@Override

提时子类要复写父类中被@Override修饰的方法。

3、@SuppressWarnings

阻止警告的意思。调用被@Deprecated注解的方法后,编译器会警告提示,而有时候开发者会忽略这种警告,他们看样子调用的地方通过@SuppressWarnings:达到目的。

4、@SafeVarargs

参数安全类型注解。它的目的是提醒开发者不要用参数做一些不安全的操作,它的存在会阻止编译器产生unchecked这样的警告。它是在Java1.7的版本中加入的。

5、@FunctionalInterface

函数式接口注解,这个是Java1.8版本引入的新特性,函数式变成很火,所以Java8也及时添加了这个特性。函数式接口(Functional Interface)就是一个具有一个方法的普通接口。

三、元注解

在创建自定义注解时,元注解负责注解自定义注解。

1、@Retention

定义注解的生命周期

1)RetentionPolicy.SOURCE

在编译阶段丢弃。这些注解在编译结束之后不再有任何意义。所以它们不会写入字节码。@Override属于这类注解。

2)RetentionPolicy.CLASS

在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方法。

3)RetentionPolicy.RUNTIME

始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方法。

2、@Target

表示注解用于什么地方

1)ElementType.CONSTRUCTOR

用于描述构造器。

2)ElementType.FIELD

成员变量、对象、属性(包括enum实例)

3)ElementType.LOCAL_VARIABLE

用于描述局部变量

4)ElementType.METHOD

用于描述方法。

5)ElementType.PACKAGE

用于描述包。

6)ElementType.PARAMETER

用于描述参数。

7)ElementType.TYPE

用于描述类、接口(包括注解类型)或enum声明。

3、@Documented

表示是否将注解信息添加在Java文档中

4、@Inherited

定义注解和子类的关系 @Inherited(class)将定义这个类被继承的。表示这个annotation类被用于该类的子类。

5、@Repeatable

指定注解可重复使用

四、自定义注解

自定义注解就是元注解负责注解的,但自定义注解时是有一些规则限制的。 Anootation类被定义为@interface,并不能再继承别的类或接口。

参数成员只能用public或默认两个访问权修饰。

参数成员只能用八种基本数据类型,byte、short、char、int、long、float、double、boolean。和String、Enum、Class、annotations等数据类型,以及这些类型的数组。

要获取类方法的字段和注解信息,必须通过java的反射技术来获取Annotations对象。

注解可不定义成员。

使用自定义注解的例子

CustomDescription注解,相当于标签。为了能贴多标签,定义注解容器CustomDescriptions。

CustomDescription

import java.lang.annotation.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Documented
@Retention(RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@Repeatable(CustomDescriptions.class)
public @interface CustomDescription {
    String description() default "";
}

CustomDescriptions

import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static java.lang.annotation.ElementType.TYPE;
@Documented
@Retention(RUNTIME)
@Target(TYPE)
@Inherited
public @interface CustomDescriptions {
    CustomDescription [] value();
}

演示注解,创建一个基类Person,来添加两个自定义注解。再进行反射输出

Person

@CustomDescription(description = "基类")
@CustomDescription(description = "人")
public class Person {
    private String Name;
    public String getName(){
        return Name;
    }
    public void setName(String name){
        Name = name;
    }
}
public class CusAnnontation {
    //TODO
    public static void main(String [] value) {
        CustomDescriptions customDescriptions = new Person().getClass().
getAnnotation(CustomDescriptions.class);
        for (CustomDescription h : customDescriptions.value()) {
            System.out.println("description:" + h.description());
        }
    }
}

为了探究他的继承性,我们再创建一个student学生类继承person类并在student中加一个自定义注解

Student

@CustomDescription(description = "学生")
public class Student extends Person {
    private  String StudentId;
    public String getStudentId(){
        return StudentId;
    }
    public void setStudentId(String studentId){
        StudentId = studentId;
    }
}

程序入口改,将从person类改成student类

public class CusAnnontation {
    //TODO
    public static void main(String [] value) {
        CustomDescriptions customDescriptions = new Student().getClass().getAnnotation(CustomDescriptions.class);
        for (CustomDescription h : customDescriptions.value()) {
            System.out.println("description:" + h.description());
        }
    }
}

执行结果

发现程序并没有使用子类的注解,@CustomDescription(description = "学生"),而是沿用了基类注解。当我们更改程序,使student的注解完全覆盖person类的注解时,即在student中添加多两个注解

@CustomDescription(description = "优秀")
@CustomDescription(description = "学生")
@CustomDescription(description = "人")
public class Student extends Person {
    private  String StudentId;
    public String getStudentId(){
        return StudentId;
    }
    public void setStudentId(String studentId){
        StudentId = studentId;
    }
}

执行结果 1

发现这时注解是使用了子类的,并没有继承到父类的注解。

提出问题

为何在使用可继承可重用的自定义注解时,当子类可重用注解个数大于父类时,就不输出继承的结果呢?

# Spring  java 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×