zoukankan      html  css  js  c++  java
  • 一次简单的自定义注解体


    很早就听说过自定义注解,自定义JSTL之类  

       为何不动手体验一次简单的自定义注解呢?


      一次简单的自定义注解entity实体验证   ,一个字段的非空验证



    先写一个验证非空的注解   (注解长的和接口很像)



    package java自定义注解ForSingleField;
    
    import java.lang.annotation.Documented;
    import java.lang.annotation.Inherited;
    import java.lang.annotation.Retention;
    import java.lang.annotation.Target;
    
    
    //有时候下面两个包可能不能自动导入,需要复制粘贴导入
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.ElementType;
    
    
    /**空指针验证类
     * @author TOSHIBA
     *
     */
    
    @Documented  
    @Inherited  
    @Target(ElementType.FIELD)  
    @Retention(RetentionPolicy.RUNTIME) 
    public @interface IsEmptyAnnotation {
    
    	public boolean isEmpty() default true;
    	public String message () default "字段不能为空";
    	
    }
    


    ------------------------------------------------------------------------------------------


    接下来写一个注解工具,来判断是否符合注解规则






    package java自定义注解ForSingleField;
    
    import java.lang.reflect.Field;
    
    
    public class AnnotationUtils {
    	
    	/**
    	 * @param value   属性值 
    	 * @param field   实体field参数
    	 */
    	public void isEmpty(Object value, Field field) {
    		IsEmptyAnnotation annotation = field
    				.getAnnotation(IsEmptyAnnotation.class);
    		if (value == null || value.equals("")) {
    			System.out.println("验证不合格--" +field.getName()+"---"+ annotation.message());
    		} else {
    			System.out.println("验证合格");
    		}
    	}
    
    
    }
    




    ---------------------------------------------------------------------------


    最后建立一个studen类来验证


    package java自定义注解ForSingleField;
    
    
    
    public class StudentInfo {
    
    	@IsEmptyAnnotation(message="学生名字不能为空")
    	private String name;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    }
    



    --------------------------------------------------------

    main方法测试

    先测试字段为空




    package java自定义注解ForSingleField;
    
    import java.lang.reflect.Field;
    
    public class StudentTest {
    
    	public static void main(String[] args) {
    		StudentInfo studentInfo =new StudentInfo();
    		AnnotationUtils annotationUtils =new AnnotationUtils();
    		
    		Class<? extends StudentInfo> clazz = studentInfo.getClass();
    		Field[] declaredFields = clazz.getDeclaredFields();
    		for (Field field : declaredFields) {
    			annotationUtils.isEmpty(studentInfo.getName(), field);
    			
    			
    		}
    		
    	}
    	
    }
    


    输出结果为 

    验证不合格--name---学生名字不能为空


    在给studentInfo.setName("tom");

    输出结果为 

    验证合格




    完结 撒花




  • 相关阅读:
    vue使用talkIngData统计
    vue项目中使用百度统计
    vue的指令修饰符
    提问:
    整理心情再投入下一个阶段
    CSS写三角形
    单行文本和多行文本超出隐藏
    清除浮动的方法
    用JS表示斐波拉契数列
    vue中使用动态挂载和懒加载,实现点击导航栏菜单弹出不同弹框
  • 原文地址:https://www.cnblogs.com/fangyuandoit/p/13713910.html
Copyright © 2011-2022 走看看