Java注解的使用
0)注解可修飾類的所有成員
1)內置注解
JDK所提供的注解,如下一些常用注解的使用:
package com.annotation; /** * @author: 肖德子裕 * @date: 2018/8/28 23:17 * @description: 注解的使用(注解可修飾類的所有成員) * 選中注解名:ctrl+鼠標左鍵可以查看源代碼 * 在源代碼中@Target表示該注解使用的范圍 * 內置注解:JDK所提供的注解 */
public class Demo1 { @Override public String toString() { return ""; } @Deprecated public static void test() { //這個注解表示這個方法不建議使用,在調用時可以看見去除線
} //@SuppressWarnings("all") //@SuppressWarnings("unchecked")
@SuppressWarnings(value = {"unchecked", "deprecation"}) public static void test1() { //這個注解表示去除這個方法的所有警告信息,有警告的話會有感嘆號 //第二個表示去除相應的警告信息 //第三個表示去除相應的多個警告
} public static void main(String[] args) { } }
2)自定義注解(關鍵字@interface)
package com.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author: 肖德子裕 * @date: 2018/8/28 23:46 * @description: 自定義注解(關鍵字@interface) * 元注解:注解其他注解 * 光定義注解沒什么用,還要通過反射解析注解才有意義 * ORM:對象關系映射 */ @Target(value = ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface SxtAnnotation { //上面第一個元注解表示這個注解只能用於方法上 //第二個一般用於反射,表示該注解在運行時有效 //studentName()代表參數,如果注解處沒有該參數會報錯,所以一般會設置一個默認值 //一般等於-1表示不存在
String studentName() default ""; int age() default 0; }
package com.annotation; /** * @author: 肖德子裕 * @date: 2018/8/28 23:51 * @description: */
public class Demo2 { @SxtAnnotation(studentName = "xdzy") public static void test() { } }
3)注解的應用
使用自定義注解表示類與表的關系
package com.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author: 肖德子裕 * @date: 2018/8/29 07:36 * @description: 使用自定義注解表示類與表的關系 */ @Target(value = ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface SxtTable { //如果只有一個參數一般取名叫value
String value(); }
對屬性進行相關注解
package com.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author: 肖德子裕 * @date: 2018/8/29 07:40 * @description: 對屬性進行相關注解 */ @Target(value = ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface SxtField { String columnName(); String type(); int length(); }
在實體類里面的注解
實體類例子
package com.annotation; /** * @author: 肖德子裕 * @date: 2018/8/29 07:34 * @description: 使用自定義注解表示類與表的關系 */ @SxtTable("tb_student") public class SexStudent { @SxtField(columnName = "sid", type = "int", length = 10) private int id; @SxtField(columnName = "sname", type = "varchar", length = 10) private String name; @SxtField(columnName = "sage", type = "int", length = 3) private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } }
4)使用反射讀取注解信息,模擬處理注解信息的流程
package com.annotation; import java.lang.annotation.Annotation; import java.lang.reflect.Field; /** * @author: 肖德子裕 * @date: 2018/8/29 07:50 * @description: 使用反射讀取注解信息,模擬處理注解信息的流程 */
public class Demo3 { public static void main(String[] args) { try { Class clazz = Class.forName("com.annotation.SexStudent"); //獲取類的所有有效注解
Annotation[] annotations = clazz.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation); } //獲取指定的注解
SxtTable st = (SxtTable) clazz.getAnnotation(SxtTable.class); System.out.println(st.value()); //獲取屬性
Field field = clazz.getDeclaredField("studentName"); //獲取指定屬性的注解
SxtField sxtField = field.getAnnotation(SxtField.class); System.out.println(sxtField.columnName() + sxtField.type() + sxtField.length()); //在上面獲取了注解的一些信息,就可以拼接sql語句,執行數據庫操作
} catch (Exception e) { e.printStackTrace(); } } }