美文网首页
【Java基础】注解Annotation

【Java基础】注解Annotation

作者: 灰色孤星 | 来源:发表于2018-10-22 10:25 被阅读0次

源代码:https://gitee.com/AgentXiao/reflection
注解要点:
1、认识注解
2、内置注解(3个)
3、自定义注解
4、使用反射处理注解(ORM)

一、认识注解

Annotation是从JDK5.0开始引入的新技术。

Annotation的作用:
1、不是程序本身,可以对程序作出解释。(这一点,跟注释没什么区别)
2、可以被其他程序(比如:编译器等)读取。(注解信息处理流程,是注解和注释的重大区别。如果没有注解信息处理流程,则注解毫无意义)

Annotation的格式:
注解是以“@注释名”在代码中存在的,还可以添加一些参数值,例如:@SuppressWarnings(value="unchecked")。

Annotation在哪里使用?
可以附加在package, class, method, field等上面,相当于给它们添加了额外的辅助信息,我们可以通过反射机制编程实现对这些元数据的访问。

二、内置注解

1、@Override
定义在java.lang.Override中,此注释只适用于修辞方法,表示一个方法声明打算重写父类中的的方法。
2、@Deprecated
定义在java.lang.Deprecated中,此注释可用于修辞方法、属性、类,表示不鼓励程序员使用这样的元素(过时了),通常是因为它很危险或存在更好的选择。
3、@SuppressWarnings
定义在java.lang.SuppressWarnings中,用来抑制编译时的警告信息。
与前两个注释有所不同,你需要添加一个参数才能正确使用,这些参数值都是已经定义好了的,我们选择性的使用就好了,参数如下:

内置注解SuppressWranings

三、自定义注解

1、元注解

注解用于注解我们编写的类、接口、方法、属性等,那么自定义的注解也会有元注解来进行注解。

  • @Target
  • @Retention
  • @Documented (不常使用)
  • @Inherited(不常使用)

(1)、@Target

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

@Target

例如:@Target(value = {ElementType.METHOD,ElementType.TYPE})
value参数是一个ElementType数组,表示该注解可以在方法和类型上使用。

(2)、@Retention

表示需要在什么级别保存该注释信息,用于描述注解的生命周期

@Retention

2、自定义注解

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

@Target(value = {ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {

    String name() default "xwd";
    int age() default 0;
    String[] schools() default {"清华大学","华南农业大学"};

}

说明:

  • 其中的每一个方法实际上是声明了一个配置参数。方法的名称就是参数的名称,返回值类型就是参数的类型(返回值类型只能是基本类型、Class、String、enum)可以通过default来声明参数的默认值。
  • 如果只有一个参数成员,一般参数名为value。
  • 注解元素必须要有值。我们定义注解元素时,经常使用空字符串、0作为默认值。也经常使用负数(比如:-1)表示不存在的含义。

四、使用反射处理注解

1、什么是ORM?

Object Relationship Mapping 对象关系映射

对象关系映射

即一个javabean类和数据库表结构的对应关系。我们可以使用注解将表结构的相关信息确定,再通过反射获得,然后生成响应的sql语句。
标记类的注解,提供表名属性(习惯上命名为value):

@Target(value = ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface table {
    String value();
}

标记属性的注解,提供列名、类型、长度等信息:

@Target(value = ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface field {
    String colName();
    String type();
    int length();
}

在javabean中使用注解:

/**
 * @ClassName Student
 * @Description 学生
 * @Author xwd
 * @Date 2018/10/22 9:44
 */
@table("tb_student")
public class Student {
    @field(colName = "id",type = "int",length = 10)
    private int id;
    @field(colName = "name",type = "varchar",length = 10)
    private String name;
    @field(colName = "age",type = "varchar",length = 3)
    private int age;

    //get和set方法
}

使用反射获得注解信息,拼接sql语句:

/**
 * @ClassName Demo
 * @Description 使用反射处理注解信息
 * @Author xwd
 * @Date 2018/10/22 9:51
 */
public class Demo {
    public static void main(String[] args) {
        try {
            Class clazz = Class.forName("pri.xiaowd.Annotation.Annotation_db.Student");

            //获得类的所有有效注解
            Annotation[] annotations = clazz.getAnnotations();
            for(Annotation a:annotations){
                System.out.println(a);
            }

            //获得类的指定注解
            table t = (table) clazz.getAnnotation(table.class);
            System.out.println(t.value());

            //获得参数的注解
            Field f = clazz.getDeclaredField("name");
            field ff = f.getAnnotation(field.class);
            System.out.println(ff.colName()+"--"+ff.type()+"--"+ff.length());

            //拼接sql语句
            String sql = "CREATE TABLE "+t.value()+"("+ff.colName()+" "+ff.type()+"("+ff.length()+"));";
            System.out.println(sql);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
}
控制台信息

相关文章

网友评论

      本文标题:【Java基础】注解Annotation

      本文链接:https://www.haomeiwen.com/subject/vcswzftx.html