Java自定義註解java
前言:這兩天看了一下Java自定義註解的內容,而後按照我本身的理解寫了兩份代碼,還挺有趣的,本文包括三個部分:註解的基礎、經過註解進行賦值(結合了工廠方法模式)、經過註解進行校驗。-----轉載 ——IT唐伯虎app
1、註解的基礎函數
1.註解的定義:Java文件叫作Annotation,用@interface表示。this
2.元註解:@interface上面按須要註解上一些東西,包括@Retention、@Target、@Document、@Inherited四種。對象
3.註解的保留策略:繼承
@Retention(RetentionPolicy.SOURCE) // 註解僅存在於源碼中,在class字節碼文件中不包含接口
@Retention(RetentionPolicy.CLASS) // 默認的保留策略,註解會在class字節碼文件中存在,但運行時沒法得到get
@Retention(RetentionPolicy.RUNTIME) // 註解會在class字節碼文件中存在,在運行時能夠經過反射獲取到源碼
4.註解的做用目標:it
@Target(ElementType.TYPE) // 接口、類、枚舉、註解
@Target(ElementType.FIELD) // 字段、枚舉的常量
@Target(ElementType.METHOD) // 方法
@Target(ElementType.PARAMETER) // 方法參數
@Target(ElementType.CONSTRUCTOR) // 構造函數
@Target(ElementType.LOCAL_VARIABLE) // 局部變量
@Target(ElementType.ANNOTATION_TYPE) // 註解
@Target(ElementType.PACKAGE) // 包
5.註解包含在javadoc中:
@Documented
6.註解能夠被繼承:
@Inherited
7.註解解析器:用來解析自定義註解。
2、經過註解進行賦值(結合了工廠方法模式)
1.自定義註解
package annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Init.java * * @author IT唐伯虎 2014年7月10日 */ @Documented @Inherited @Target({ ElementType.FIELD, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) public @interface Init { public String value() default ""; }
2.在數據模型使用註解
package model; import annotation.Init; /** * User.java * * @author IT唐伯虎 2014年7月10日 */ public class User { private String name; private String age; public String getName() { return name; } @Init(value = "liang") public void setName(String name) { this.name = name; } public String getAge() { return age; } @Init(value = "23") public void setAge(String age) { this.age = age; } }
3.用「構造工廠」充當「註解解析器」
package factory; import java.lang.reflect.Method; import annotation.Init; import model.User; /** * UserFactory.java * * @author IT唐伯虎 2014年7月10日 */ public class UserFactory { public static User create() { User user = new User(); // 獲取User類中全部的方法(getDeclaredMethods也行) Method[] methods = User.class.getMethods(); try { for (Method method : methods) { // 若是此方法有註解,就把註解裏面的數據賦值到user對象 if (method.isAnnotationPresent(Init.class)) { Init init = method.getAnnotation(Init.class); method.invoke(user, init.value()); } } } catch (Exception e) { e.printStackTrace(); return null; } return user; } }
4.運行的代碼
package app; import java.lang.reflect.InvocationTargetException; import factory.UserFactory; import model.User; /** * Test.java * * @author IT唐伯虎 2014年7月10日 */ public class Test { public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { User user = UserFactory.create(); System.out.println(user.getName()); System.out.println(user.getAge()); } }
5.運行結果
liang 23
3、經過註解進行校驗
1.自定義註解
package annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Validate.java * * @author IT唐伯虎 2014年7月11日 */ @Documented @Inherited @Target({ ElementType.FIELD, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) public @interface Validate { public int min() default 1; public int max() default 10; public boolean isNotNull() default true; }
2.在數據模型使用註解
package model; import annotation.Validate; /** * User.java * * @author IT唐伯虎 2014年7月11日 */ public class User { @Validate(min = 2, max = 5) private String name; @Validate(isNotNull = false) private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
3.註解解析器
package check; import java.lang.reflect.Field; import annotation.Validate; import model.User; /** * UserCheck.java * * @author IT唐伯虎 2014年7月11日 */ public class UserCheck { public static boolean check(User user) { if (user == null) { System.out.println("!!校驗對象爲空!!"); return false; } // 獲取User類的全部屬性(若是使用getFields,就沒法獲取到private的屬性) Field[] fields = User.class.getDeclaredFields(); for (Field field : fields) { // 若是屬性有註解,就進行校驗 if (field.isAnnotationPresent(Validate.class)) { Validate validate = field.getAnnotation(Validate.class); if (field.getName().equals("age")) { if (user.getAge() == null) { if (validate.isNotNull()) { System.out.println("!!年齡可空校驗不經過:不可爲空!!"); return false; } else { System.out.println("年齡可空校驗經過:能夠爲空"); continue; } } else { System.out.println("年齡可空校驗經過"); } if (user.getAge().length() < validate.min()) { System.out.println("!!年齡最小長度校驗不經過!!"); return false; } else { System.out.println("年齡最小長度校驗經過"); } if (user.getAge().length() > validate.max()) { System.out.println("!!年齡最大長度校驗不經過!!"); return false; } else { System.out.println("年齡最大長度校驗經過"); } } if (field.getName().equals("name")) { if (user.getName() == null) { if (validate.isNotNull()) { System.out.println("!!名字可空校驗不經過:不可爲空!!"); return false; } else { System.out.println("名字可空校驗經過:能夠爲空"); continue; } } else { System.out.println("名字可空校驗經過"); } if (user.getName().length() < validate.min()) { System.out.println("!!名字最小長度校驗不經過!!"); return false; } else { System.out.println("名字最小長度校驗經過"); } if (user.getName().length() > validate.max()) { System.out.println("!!名字最大長度校驗不經過!!"); return false; } else { System.out.println("名字最大長度校驗經過"); } } } } return true; } }
4.運行的代碼
package app; import check.UserCheck; import model.User; /** * Test.java * * @author IT唐伯虎 2014年7月11日 */ public class Test { public static void main(String[] args) { User user = new User(); user.setName("liang"); user.setAge("1"); System.out.println(UserCheck.check(user)); } }
5.運行結果
名字可空校驗經過 名字最小長度校驗經過 名字最大長度校驗經過 年齡可空校驗經過 年齡最小長度校驗經過 年齡最大長度校驗經過 true