元注解:
元注解的作用就是负责注解其他注解。Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明。Java5.0定义的元注解:
1.@Target,
2.@Retention,
3.@Documented,
4.@Inherited
这些类型和它们所支持的类在java.lang.annotation包中可以找到。下面我们看一下每个元注解的作用和相应分参数的使用说明。以下为一个简单场景的应用: 1.定义注解:
@Target(TYPE)
@Retention(RUNTIME)
public @interface Table {
/**
* (Optional) The name of the table.
* <p/>
* Defaults to the entity name.
*/
String name() default "";
}
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface Column {
/**
* (Optional) The name of the column. Defaults to
* the property or field name.
*/
String name() default "";
}
2、定义实体类:
@Table(name = "t_s_user")
public class User {
@Column(name="name")
private String name;
@Column(name="pwd")
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
3、运行:
public static void print() {
System.out.println("table's name:" + User.class.getAnnotation(Table.class).name());
Field[] fields = User.class.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
System.out.println("field's type:" + field.getType().getName());
System.out.println("field's columnName:" + field.getAnnotation(Column.class).name());
}
}
关于注解的详细介绍:http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html