亲宝软件园·资讯

展开

浅谈java反射和自定义注解的综合应用实例

人气:0

前言

前几天学习了反射和自定义注解,刚好工作中遇到一个小问题:前台传递到后台的必填字段为空,导致不能插入数据库。就是这样一个小问题,让我考虑到是否可以做一个通用的方法,让前台传递过来的必填字段在后台也校验一遍,如果传递为空,则把响应字段返回提示。因此,我考虑的是用注解的方式,在必填字段上面定义,利用反射得到必填字段的字段名,判断是否为空,并返回响应的信息。

需求模拟

假设客户有:姓名,年龄,地址,手机号码,SFZ号等信息,而我们是做金融业务,所以关键是看客户的三要素:姓名,SFZ号,手机号码。我们要保证前台传递过来的这三个值不为空。
废话不多说,直接上代码。只看红框里面的即可。

目录结构

客户信息类:Customer

这个是个实体类,我们在:姓名,SFZ号码,手机号码上都用了我们的自定义注解。
 

package com.dao.chu.po; 
 
/** 
 * 
* <p>Title: Customer</p> 
* <p>Description:客户信息实体 </p> 
 */ 
public class Customer { 
 
  private int id; 
  @IsRequired 
  private String name; // 姓名 
  @IsRequired 
  private String idnum; // SFZ号码 
  @IsRequired 
  private String phone; // 手机号 
  private String sex; // 性别 
  private int age; // 年龄 
  private String address; // 地址 
 
  @Override 
  public String toString() { 
    return "Customer [id=" + id + ", name=" + name + ", idnum=" + idnum 
        + ", phone=" + phone + ", sex=" + sex + ", age=" + age 
        + ", address=" + address + "]"; 
  } 
 
  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 String getIdnum() { 
    return idnum; 
  } 
 
  public void setIdnum(String idnum) { 
    this.idnum = idnum; 
  } 
 
  public String getPhone() { 
    return phone; 
  } 
 
  public void setPhone(String phone) { 
    this.phone = phone; 
  } 
 
  public String getSex() { 
    return sex; 
  } 
 
  public void setSex(String sex) { 
    this.sex = sex; 
  } 
 
  public int getAge() { 
    return age; 
  } 
 
  public void setAge(int age) { 
    this.age = age; 
  } 
 
  public String getAddress() { 
    return address; 
  } 
 
  public void setAddress(String address) { 
    this.address = address; 
  } 
   
} 

自定义注解类:IsRequired
 

package com.dao.chu.po;  
import java.lang.annotation.ElementType; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.annotation.Target; 
 
/** 
 * 
* <p>Title: IsRequired</p> 
* <p>Description: 字段是否必填 </p> 
 */ 
@Retention(value = RetentionPolicy.RUNTIME)  
@Target(value = {ElementType.FIELD})  
public @interface IsRequired  
{  
  /** 
   *  
   
  * <p>Title: isRequired</p> 
  * <p>Description:true:必填 false:非必填 </p> 
  * @return 
   */ 
  boolean isRequired() default true;  
}  

关键工具类:PoUtils

我们在这个类里面主要用了反射的知识,得到带有自定义注解的字段,并取得这个对象的值进行判断

package com.dao.chu.po;  
  
import java.beans.IntrospectionException; 
import java.beans.PropertyDescriptor; 
import java.lang.reflect.Field; 
import java.lang.reflect.InvocationTargetException; 
 
import javax.jws.WebResult; 
 
import com.sun.xml.internal.ws.util.StringUtils; 
  
/** 
 * 
* <p>Title: PoUtils</p> 
* <p>Description:Po操作工具类 </p> 
 */ 
@SuppressWarnings("unused")  
public class PoUtils  
{  
    
  /** 
  * <p>Title: getProperties</p> 
  * <p>Description: 获取javabean属性通用方法 </p> 
  * @param t 
  * @param beanName 
  * @return 
  * @throws IllegalAccessException 
  * @throws IllegalArgumentException 
  * @throws InvocationTargetException 
  * @throws IntrospectionException 
   */ 
  private static <T> Object getProperties(T t, String beanName)  
    throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException  
  {  
    Object nameValue = new PropertyDescriptor(beanName, t.getClass()).getReadMethod().invoke(t);  
    return nameValue;  
  }  
    
  /** 
  * <p>Title: IsFieldBlank</p> 
  * <p>Description:判断前台传过来的必填字段是否为空 ,不正确则将相应字段返回 </p> 
  * @param t 
  * @return 
  * @throws IllegalAccessException 
  * @throws IllegalArgumentException 
  * @throws InvocationTargetException 
  * @throws IntrospectionException 
   */ 
  public static <T> RespBody IsFieldBlank(T t)  
    throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException  
  {  
    RespBody respBody = new RespBody(); 
    StringBuffer sb = new StringBuffer();  
      
    Field[] declaredFields = t.getClass().getDeclaredFields();  
    for (Field field : declaredFields)  
    {  
      field.setAccessible(true);  
      String name = field.getName();  
      boolean fieldHasAnno = field.isAnnotationPresent(IsRequired.class);  
      if (fieldHasAnno)  
      {  
        IsRequired annotation = field.getAnnotation(IsRequired.class);  
        boolean required = annotation.isRequired();  
        if (required)  
        {  
          Object value = getProperties(t, name);  
          if (null == value)  
          {  
            sb.append(name + ",");  
          }  
        }  
      }  
    }  
      
    if (null==sb.toString()||"".equals(sb.toString()))  
    {  
      respBody.isSuccess(); 
    }  
      
    respBody.setSuccess(false); 
    respBody.setMsg(sb.toString().substring(0,sb.toString().lastIndexOf(",")) + " is required");  
    return respBody;  
  }  
}  

RespBody:响应实体类

封装了响应的成功失败以及一些信息

 package com.dao.chu.po; 
 
/** 
 * 
* <p>Title: RespBody</p> 
* <p>Description: 响应实体类</p> 
 */ 
public class RespBody  
{  
  private boolean isSuccess = true;  
    
  private String msg;  
    
  private Object data;  
    
  public boolean isSuccess()  
  {  
    return isSuccess;  
  }  
    
  public void setSuccess(boolean isSuccess)  
  {  
    this.isSuccess = isSuccess;  
  }  
    
  public String getMsg()  
  {  
    return msg;  
  }  
    
  public void setMsg(String msg)  
  {  
    this.msg = msg;  
  }  
    
  public Object getData()  
  {  
    return data;  
  }  
    
  public void setData(Object data)  
  {  
    this.data = data;  
  }  
    
  public RespBody(boolean isSuccess, String msg, Object data)  
  {  
    super();  
    this.isSuccess = isSuccess;  
    this.msg = msg;  
    this.data = data;  
  }  
    
  public RespBody(boolean isSuccess, String msg)  
  {  
    super();  
    this.isSuccess = isSuccess;  
    this.msg = msg;  
  }  
    
  public RespBody()  
  {  
  }  
    
  @Override  
  public String toString()  
  {  
    return "ReturnBody [isSuccess=" + isSuccess + ", msg=" + msg + ", data=" + data + "]";  
  }  
}  

测试类:IsRequiredTest

 package com.dao.chu.po; 
 
/** 
 * 
* <p>Title: IsRequiredTest</p> 
* <p>Description: 必填成员变量测试类</p> 
 */ 
public class IsRequiredTest { 
 
  public static void main(String[] args) { 
     
    Customer customer = new Customer(); 
     
    try { 
      //=========第一次不赋值========== 
      RespBody respBody = PoUtils.IsFieldBlank(customer); 
      //不通过则返回提示信息 
      if (!respBody.isSuccess()) { 
        System.out.println("1."+respBody.getMsg()); 
      } 
       
      //=========第二次给姓名赋值========== 
      customer.setName("张三"); 
      respBody = PoUtils.IsFieldBlank(customer); 
      //不通过则返回提示信息 
      if (!respBody.isSuccess()) { 
        System.out.println("2."+respBody.getMsg()); 
      } 
       
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
  } 
   
   
   
} 

输出结果

第一次三个值都为空,提示三个都是必填的,第二次因为姓名赋值了,所以提示另外两个是必填的,本次实验宣告结束,本人知识有限,若有更好的方法欢迎指正

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论