注解的创建与使用

#1.注解的定义
注解就是对一个类,或者一个成员,一个方法加上一个标签,通过对于标签的解析,我们可以得到注解里面的信息,有时我们可以利用注解来代替XML文件,因为XML文件里面也是通过标签存储了一定的数据。相信有人看到这里是生涩的,本文主要讲在代码层面怎么去写一个注解,并且去解析它,如果想详细得知关于注解的相关概念清看这位大佬的博客
链接:https://blog.csdn.net/javazejian/article/details/71860633

#2.创建一个注解文件
我们使用的是eclipse
1.跟建立一个类的方法一样只不过我们选择Annotation
在这里插入图片描述
2. 点开后我们会得到这样的页面
在这里插入图片描述
3.其中Retention是生命周期,具体是什么请看上面的链接,我们主要讲怎么去解析和声明注解

4.Target是目标,这个很好理解,给类的注解就是类注解,给方法的注解就叫方法注解等等以此类推。
这里我只解释一些像我这样的小白常用的
Type 类注解
Field 成员注解
Method 方法注解
Parameter 参数注解

5.我们先建立一个类注解
Target里面选择Type并且输入注解名
我们要用到反射机制解析 Retention 选择 Runtime
我们会看到这样的结果

package com.mec.boke;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

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

@Retention(RUNTIME)
@Target({ TYPE, FIELD })
public @interface ClassAnnotaion {

}

接下来我们给这个注解里面写上一个成员(注意这里的书写格式有一点特殊)

package com.mec.Demo;

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

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

一定要注意这里的书写格式不是String value;这样定义一个成员
要写成String value();这里的写法大家接受就好

6.为了使我们注解的解析例子更加丰富我们在写一个成员注解
记住
Target选择Field
Retention 选择 Runtime

package com.mec.Demo;

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

@Target(value = {ElementType.FIELD})

@Retention(RetentionPolicy.RUNTIME)
public @interface PropertiesAnnotation {
	    String  columnName();              

	    String  type();                          

	    int length();
 
}

7.接下来使用我们上述完成的两个自定义注解去写一个类

package com.mec.annotaionDemo;

import com.mec.Demo.ClassAnnotation;
import com.mec.Demo.PropertiesAnnotation;

@ClassAnnotation("Table")
public class Demo {
	@PropertiesAnnotation(columnName = "name",type = "String", length = 10)

	 private String name;
	@PropertiesAnnotation(columnName = "id",type = "String", length = 8)
	 private String id;
	@PropertiesAnnotation(columnName = "name",type = "String", length = 7)
	 private int address;   

}

这里其实并不复杂 比如要填加类注解时只需要在类上面输入@ + 你的注解名,可以用ALT+ / 快捷键自动补全,十分的方便
成员注解使用方式同理。

最后给你相应的注解成员赋值就行。

8.接下来我们去解析这个拥有注解的类(重点)

package com.mec.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import com.mec.Demo.ClassAnnotation;
import com.mec.Demo.PropertiesAnnotation;

public class Test {
	public static void main(String[] args) {
		 try {
			//加载拥有注解的那个类
			Class clazz = Class.forName("com.mec.annotaionDemo.Demo");
			//这里可以抽象的理解成得到这个类的注解
			ClassAnnotation ca = (ClassAnnotation)clazz.getAnnotation(ClassAnnotation.class);
			//输出我们看一下
			System.out.println(ca);
			System.out.println(ca.value());
			
			//通过反射机制我们得到Demo类中名字为id的成员
			Field f = clazz.getDeclaredField("id");
			//得到成员的注解
			PropertiesAnnotation pa = f.getAnnotation(PropertiesAnnotation.class);
			
			//输出我们看一下
			System.out.println("----------------");
			System.out.println(pa);
			System.out.println(pa.columnName()+" ----- "+ pa.type()+"-------"+pa.length());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		}              
	}
}

运行结果为:
运行结果
当然关于注解还有很多Java本身给的方法,我也就不一一列举了,对与这里面方法的学习,我们不能像背书一样去记它,而是像要查字典一样学一个用一个。

这里我也仅仅是说明了注解的最基本在语法层面的用法,希望对于刚刚接触注解的同学有一定的帮助。

欲带皇冠 必承其重,希望大家取得成功!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值