java注解

java注解

与注释不同,注解是给程序看的,注释是给程序员看的。

作用

可以被其他程序读取,对程序作出解释

可以在package,class,method,field上面,相当于添加了额外的辅助信息,可以通过反射机制鞭策对这些元数据的访问。

内置注解

在java.lang.xxx中

//重写
@Override 


//不推荐使用,过期
@Deprecated  

//抑制警告
@SuppressWarnings
元注解

负责注解其他的注解,有四个元注解,分别是

//用于描述注解的使用范围,PACKAGE,TYPE,METHOD,FILED,CONSTRUCT。。。。。
@Target()

//在什么级别报错该注释信息,用于描述注解的生命周期
@Retention
(Source源码<class编译<runtime默认)

//注解将包含杂javadoc中
@Doucument

//说明子类可以继承父类中的注解
@Inherited

注解的定义

注解通过 @interface 关键字进行定义。

自动继承java.lang.annotation.Annotation接口

public @interface TestAnnoation {
		定义内容
}
public @interface TestAnnoation {

	// 注解的属性也叫做成员变量,不是方法而是参数 类型 参数名
	// default 默认值 如果-1表示不存在
	int id() default 1;

	String msg(); // 注解的成员变量

}
调用自定义注解
@TestAnnoation(id = 0, msg = "")
public class Test extends Object {

	@Override
	public String toString() {
		return super.toString();
	}

	// 不推荐使用
	@Deprecated
	public static void test() {
		System.out.println("Deprecated");
	}

	// 抑制警告
	@SuppressWarnings("all")
	public void test02() {
		List list = new ArrayList<>();
	}

	public static void main(String[] args) {

		test();
	}
}

利用注解和反射完成类和表的映射关系

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

/**
 * 通过反射获取注解
 * 
 * 
 * 映射User类
 * 
 * @author zwm
 *
 */
public class ReflectionDemo11 {
	public static void main(String[] args) throws ClassNotFoundException {
		Class class1 = Class.forName("User2");

		// 通过反射获取注解
		Annotation[] annotations = class1.getAnnotations();
		for (Annotation annotation : annotations) {
			System.out.println(annotation);
		}

		// 获取注解的值
		table table = (table) class1.getAnnotation(table.class);
		String value = table.value();
		System.out.println(value);

		// 获取类属性的值
		filed filed = (filed) class1.getAnnotation(filed.class);
		System.out.println(filed.columnName());
		System.out.println(filed.length());
		System.out.println(filed.type());
	}
}

@table("db_user")
class User2 {
	@filed(columnName = "name", type = "varchar", length = 10)
	private String name;
	@filed(columnName = "age", type = "int", length = 10)
	private int age;

	public User2() {
		super();
		// TODO Auto-generated constructor stub
	}

	public User2(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "User2 [name=" + name + ", age=" + age + "]";
	}
}

//类注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface table {
	// 表名
	String value();
}

//属性、字段注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface filed {
	String columnName();

	String type();

	int length();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值