JAVA中注解

注解入门

注解(Annotation)的发展:

  • 注解是从JDK5.0开始引入的新技术

注解(Annotation)的作用:

  • 不属于程序本身,可以对程序作出解释(这一点跟注释没什么区别)
  • 可以被其他程序(如编译器)读取

注解(Annotation)的格式:

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

注解(Annotation)使用位置:

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

 

内置注解

(1)@Override

定义在java.lang.Override中,此注释只适用于修饰方法,表示一个方法声明打算重写父类中的另一个方法的声明。

public class Test01 {
	
	@Override
	public String toString(){
		return null;
	}

}

说明:Test01默认继承Object,加上注解“@Override”表示该方法重写父类中的toString方法。

 

(2)@Deprecated

定义在java.lang.Deprecated中,此注解可用于修饰方法,属性,类。表示不鼓励程序员使用这样的元素,通常是因为它很危险或存在更好的选择。

public class Test01 {
	
	@Deprecated
	public void test(){	
	}

}

说明:test方法加上注解“@Deprecated”表示该方法不建议使用,在开发环境中会显示删除线。

 

(3)@SuppressWarnings

定义在java.lang.SuppressWarnings中,用来抑制编译时的警告信息,该注释需要添加一个参数才可以使用,这些参数值如下:

使用范例:

@SuppressWarnings("unchecked")

@SuppressWarnings(value={"unchecked","deprecation"})

@SuppressWarnings("all")
public static void test01(){
	List list1=new ArrayList();
	List list2=new ArrayList();
	List list3=new ArrayList();
	List list4=new ArrayList();
	List list5=new ArrayList();
}

说明:使用List集合,没有加泛型的信息时,会出现黄色警告信息,加上注解“@SuppressWarnings("all")”后,可以抑制所有黄色警告信息。

 

元注解

元注解的作用就是负责注解其他注解。Java定义了4个标注的元注解类型(meta-annotation),它们被用来提供对其它注解类型作说明。这些类型和它们所支持的类再java.lang.annotation包中可以找到,四个元注解包括:@Target、@Retention、@Documented、@Inherited

(1)@Target

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

使用范例:@Target(value=ElementType.TYPE)

 

(2)@Retention

作用:表示需要什么级别保存该注解信息,用于描述注解的声明周期

使用范例:@Retention(RetentionPolicy.RUNTIME)

 

自定义注解

使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口

要点:@interface用来声明一个注解

格式:

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

实例:

@Target(value={ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	
	String studentName() default "";
	int age() default 0;
	int id() default -1;
	String[] schools() default {"清华大学","中南大学"};
	
}

测试:

public class Test02 {
	
	//注解使用默认参数
	@MyAnnotation
	public void test(){
	}
	
	//注解使用显式定义值
	@MyAnnotation(studentName="老高",age=12,id=1001,schools={"北京大学","湖南大学"})
	public void test1(){
	}

}

 

实例:模拟ORM注解使用

需求:hibernate框架中,使用ORM(Object Relationship Mapping),将类与表结构对应,属性与字段对应,对象与记录对应。

现使用注解完成类和表结构的映射关系,定义注解处理流程读取这些注解。

第一步:创建学生类,包括id,studentName,age三个属性

public class Student {
	
	private int id;
	private String studentName;
	private int age;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

第二步:定义注解,对Student类进行解析,对应数据库中的t_student数据表

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

第三步:定义注解,对属性进行解析,对应数据表中的字段

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

第四步:对Student类使用注解

@Table("t_student")
public class Student {
	
	@Field(columnName="id",type="int",length=10)
	private int id;
	@Field(columnName="studentName",type="varchar",length=10)
	private String studentName;
	@Field(columnName="id",type="int",length=3)
	private int age;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

第五步:书写解析程序,读取注解(一般框架中已经写好了,只需要使用注解即可)

//使用反射读取注解的信息,模拟处理注解信息的流程
public class Demo {
	public static void main(String[] args) {
		try {
			Class clazz=Class.forName("an.itcast.orm.Student");
			
			//获取Student类的所有有效注解
			Annotation[] annotations =clazz.getAnnotations();
			for (Annotation annotation : annotations) {
				System.out.println(annotation);
			}
			
			//获得类的指定注解
			SxtTable table=(SxtTable) clazz.getAnnotation(SxtTable.class);
			System.out.println(table.value());
			
			//获得类的属性的注解
			Field field=clazz.getDeclaredField("studentName");
			SxtField sxtField=field.getAnnotation(SxtField.class);
			System.out.println(sxtField.columnName());
			System.out.println(sxtField.type());
			System.out.println(sxtField.length());
			
			//根据获得的表名,字段的信息,拼出DDL语句,然后,使用JDBC执行这个SQL,在数据库中农生成相关的表
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值