Java注解
前面都是抄的别人的,发点自己的东西
1 2 3 4 5 6 7 8 9 10 11 12 13 | package 注解.反射读取; 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 (value = RetentionPolicy.RUNTIME) public @interface SxtTable { String value(); } |
package 注解. 反射读取;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;//用来说明 SXtStudent 里面的属性特性
@Target(value = ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SxtFiled
{
String columnName();
String type();
int length();
}
package 注解. 反射读取;@SxtTable("tb_table")
public class SxtStudent
{
@SxtFiled(columnName = "id",type="int",length = 10)
private int id;
//对属性做了说明,
@SxtFiled(columnName = "sname",type="varchar",length = 10)
private String StudentName;@SxtFiled(columnName </span>= "age",type="int",length = 3<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age; </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getId() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> id; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setId(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> id) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.id =<span style="color: rgba(0, 0, 0, 1)"> id; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getStudentName() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> StudentName; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setStudentName(String studentName) { StudentName </span>=<span style="color: rgba(0, 0, 0, 1)"> studentName; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getAge() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> age; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setAge(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age; }
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package 注解.反射读取; import java.lang.annotation.Annotation; import java.lang.reflect.Field; //反射读取ANN public class DEMO { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException { Class Clazz= Class.forName( "注解.反射读取.SxtStudent" ); //包含对应类的全部信息 包含注解 // Annotation annot[]=Clazz.getAnnotations();//获取注解 // for(Annotation a:annot) // { // System.out.println(a); // } //直接获得特定的注解 // SxtTable st= (SxtTable) Clazz.getAnnotation(SxtTable.class); // System.out.println(st); //获得对应类的属性的注解 Field f=Clazz.getDeclaredField( "StudentName" ); System.out.println(f); SxtFiled sxtf=f.getAnnotation(SxtFiled. class ); System.out.println(sxtf); System.out.println(sxtf.columnName()); System.out.println(sxtf.length()); System.out.println(sxtf.type()); } } |