import java.lang.annotation.Annotation; import java.lang.reflect.Field; public class AnnotationTest { public static void main(String[] args) throws Exception { Class c = Class.forName("Customer"); c = Customer.class; Table table = (Table)c.getDeclaredAnnotation(Table.class); System.out.println(table.value()); Annotation[] annotations = c.getDeclaredAnnotations(); for(Annotation annotation : annotations) { System.out.println(annotation.annotationType().getName()); } Field name = c.getDeclaredField("name"); Column column = name.getDeclaredAnnotation(Column.class); System.out.println(column.columnName()); System.out.println(column.columnType()); } }
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class AnnotationTest
{
public static void main(String[] args) throws Exception
{
Class c = Class.forName("Customer");
c = Customer.class;
Table table = (Table)c.getDeclaredAnnotation(Table.class);
System.out.println(table.value());
Annotation[] annotations = c.getDeclaredAnnotations();
for(Annotation annotation : annotations)
{
System.out.println(annotation.annotationType().getName());
}
Field name = c.getDeclaredField("name");
Column column = name.getDeclaredAnnotation(Column.class);
System.out.println(column.columnName());
System.out.println(column.columnType());
}
}
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.FIELD; @Target({FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface Column { String columnName(); String columnType(); }
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.FIELD;
@Target({FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Column
{
String columnName();
String columnType();
}
@Table(value = "t_Customer") public class Customer { @Column(columnName = "Customer_Name",columnType = "varchar(15)") private String name; @Column(columnName = "age",columnType = "int") public int age; public Customer() { System.out.println("Customer"); } public Customer(int age) { this.age = age; } private Customer(String name,int age) { this.age = age; this.name = name; } }
@Table(value = "t_Customer")
public class Customer
{
@Column(columnName = "Customer_Name",columnType = "varchar(15)")
private String name;
@Column(columnName = "age",columnType = "int")
public int age;
public Customer()
{
System.out.println("Customer");
}
public Customer(int age)
{
this.age = age;
}
private Customer(String name,int age)
{
this.age = age;
this.name = name;
}
}文章来源:https://www.toymoban.com/news/detail-607042.html
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.*; @Target({TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Table { String value() default "Stop"; }
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
@Target({TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Table
{
String value() default "Stop";
}文章来源地址https://www.toymoban.com/news/detail-607042.html
到了这里,关于JAVA使用反射机制和注解实现对信息的处理-----JAVA入门基础教程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!