通过java反射,封装bean

时间:2021-03-11 18:45:28

上周有一个封装javabean的流程,成员变量有好多,一路写if(!=null){setXXX} 真的好麻烦,有时候一不小心还会漏掉,很是麻烦,就想到用反射的方法自动进行拼装bean。

bean

package com.jiaotd.reflect;

/**
* @author jiaotd@asiainfo.com
* @since 2016年3月14日 上午9:12:47
*/
public class NewCustInfo {
public String dataId;
public String custId;
public String provCode;
public String regionId;
public String billId;
public String custName;
public String custCertType;
public String custCertNo;
public String custCertAddr;
public String certValiddate;
public String certExpdate;
public String gender;
public String nation;
public String birthday;
public String linkaddr;
public String linkman;
public String linkphone;
public String issuingAuthority;
public String fistGetDate;
public String validduration;
public String archiveNo;
public String state;
public String createDate;
public String authType;
public String notes;
public String ext1;
public String ext2;
public String ext3;
public String ext4;
public String ext5;
public String ext6;
public String ext7;
public String ext8;
public String ext9;
public String ext10; public NewCustInfo() {
} public String getDataId() {
return dataId;
} public void setDataId(String dataId) {
this.dataId = dataId;
} public String getCustId() {
return custId;
} public void setCustId(String custId) {
this.custId = custId;
} public String getProvCode() {
return provCode;
} public void setProvCode(String provCode) {
this.provCode = provCode;
} public String getRegionId() {
return regionId;
} public void setRegionId(String regionId) {
this.regionId = regionId;
} public String getBillId() {
return billId;
} public void setBillId(String billId) {
this.billId = billId;
} public String getCustName() {
return custName;
} public void setCustName(String custName) {
this.custName = custName;
} public String getCustCertType() {
return custCertType;
} public void setCustCertType(String custCertType) {
this.custCertType = custCertType;
} public String getCustCertNo() {
return custCertNo;
} public void setCustCertNo(String custCertNo) {
this.custCertNo = custCertNo;
} public String getCustCertAddr() {
return custCertAddr;
} public void setCustCertAddr(String custCertAddr) {
this.custCertAddr = custCertAddr;
} public String getCertValiddate() {
return certValiddate;
} public void setCertValiddate(String certValiddate) {
this.certValiddate = certValiddate;
} public String getCertExpdate() {
return certExpdate;
} public void setCertExpdate(String certExpdate) {
this.certExpdate = certExpdate;
} public String getGender() {
return gender;
} public void setGender(String gender) {
this.gender = gender;
} public String getNation() {
return nation;
} public void setNation(String nation) {
this.nation = nation;
} public String getBirthday() {
return birthday;
} public void setBirthday(String birthday) {
this.birthday = birthday;
} public String getLinkaddr() {
return linkaddr;
} public void setLinkaddr(String linkaddr) {
this.linkaddr = linkaddr;
} public String getLinkman() {
return linkman;
} public void setLinkman(String linkman) {
this.linkman = linkman;
} public String getLinkphone() {
return linkphone;
} public void setLinkphone(String linkphone) {
this.linkphone = linkphone;
} public String getIssuingAuthority() {
return issuingAuthority;
} public void setIssuingAuthority(String issuingAuthority) {
this.issuingAuthority = issuingAuthority;
} public String getFistGetDate() {
return fistGetDate;
} public void setFistGetDate(String fistGetDate) {
this.fistGetDate = fistGetDate;
} public String getValidduration() {
return validduration;
} public void setValidduration(String validduration) {
this.validduration = validduration;
} public String getArchiveNo() {
return archiveNo;
} public void setArchiveNo(String archiveNo) {
this.archiveNo = archiveNo;
} public String getState() {
return state;
} public void setState(String state) {
this.state = state;
} public String getCreateDate() {
return createDate;
} public void setCreateDate(String createDate) {
this.createDate = createDate;
} public String getAuthType() {
return authType;
} public void setAuthType(String authType) {
this.authType = authType;
} public String getNotes() {
return notes;
} public void setNotes(String notes) {
this.notes = notes;
} public String getExt1() {
return ext1;
} public void setExt1(String ext1) {
this.ext1 = ext1;
} public String getExt2() {
return ext2;
} public void setExt2(String ext2) {
this.ext2 = ext2;
} public String getExt3() {
return ext3;
} public void setExt3(String ext3) {
this.ext3 = ext3;
} public String getExt4() {
return ext4;
} public void setExt4(String ext4) {
this.ext4 = ext4;
} public String getExt5() {
return ext5;
} public void setExt5(String ext5) {
this.ext5 = ext5;
} public String getExt6() {
return ext6;
} public void setExt6(String ext6) {
this.ext6 = ext6;
} public String getExt7() {
return ext7;
} public void setExt7(String ext7) {
this.ext7 = ext7;
} public String getExt8() {
return ext8;
} public void setExt8(String ext8) {
this.ext8 = ext8;
} public String getExt9() {
return ext9;
} public void setExt9(String ext9) {
this.ext9 = ext9;
} public String getExt10() {
return ext10;
} public void setExt10(String ext10) {
this.ext10 = ext10;
} @Override
public String toString() {
return "NewCustInfo [dataId=" + dataId + ", custId=" + custId
+ ", provCode=" + provCode + ", regionId=" + regionId
+ ", billId=" + billId + ", custName=" + custName
+ ", custCertType=" + custCertType + ", custCertNo="
+ custCertNo + ", custCertAddr=" + custCertAddr
+ ", certValiddate=" + certValiddate + ", certExpdate="
+ certExpdate + ", gender=" + gender + ", nation=" + nation
+ ", birthday=" + birthday + ", linkaddr=" + linkaddr
+ ", linkman=" + linkman + ", linkphone=" + linkphone
+ ", issuingAuthority=" + issuingAuthority + ", fistGetDate="
+ fistGetDate + ", validduration=" + validduration
+ ", archiveNo=" + archiveNo + ", state=" + state
+ ", createDate=" + createDate + ", authType=" + authType
+ ", notes=" + notes + ", ext1=" + ext1 + ", ext2=" + ext2
+ ", ext3=" + ext3 + ", ext4=" + ext4 + ", ext5=" + ext5
+ ", ext6=" + ext6 + ", ext7=" + ext7 + ", ext8=" + ext8
+ ", ext9=" + ext9 + ", ext10=" + ext10 + "]";
} }

具体方法

需要注意的是

Method method = clazz.getMethod(key, String.class);

第一个参数是方法名,第二参数是一个多参数,即给方法需要的所有参数的类型。

本bean对象都是String类型的,所以可以这写,其余情景可以自行变形。

传入你要分装的bean对象和值

method.invoke(newcustinfo,map.get(s));

package com.jiaotd.reflect;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* @since 2016年3月14日 上午9:01:41
*/
public class CreateBean { public static List<String> paramBodyKey = new ArrayList<String>();
static {
paramBodyKey.add("dataId");
paramBodyKey.add("custId");
paramBodyKey.add("provCode");
paramBodyKey.add("regionId");
paramBodyKey.add("billId");
paramBodyKey.add("custName");
paramBodyKey.add("custCertType");
paramBodyKey.add("custCertNo");
paramBodyKey.add("custCertAddr");
paramBodyKey.add("certValiddate");
paramBodyKey.add("certExpdate");
paramBodyKey.add("gender");
paramBodyKey.add("nation");
paramBodyKey.add("birthday");
paramBodyKey.add("linkaddr");
paramBodyKey.add("linkman");
paramBodyKey.add("linkphone");
paramBodyKey.add("issuingAuthority");
paramBodyKey.add("fistGetDate");
paramBodyKey.add("validduration");
paramBodyKey.add("archiveNo");
paramBodyKey.add("state");
paramBodyKey.add("createDate");
paramBodyKey.add("authType");
paramBodyKey.add("notes");
paramBodyKey.add("ext1");
paramBodyKey.add("ext2");
paramBodyKey.add("ext3");
paramBodyKey.add("ext4");
paramBodyKey.add("ext5");
paramBodyKey.add("ext6");
paramBodyKey.add("ext7");
paramBodyKey.add("ext8");
paramBodyKey.add("ext9");
paramBodyKey.add("ext10");
} public static Map<String, String> padBean(String str){
Map<String, String> map = new HashMap<String,String>();
String[] strs = str.split("\\|");
for(int i=0;i<strs.length;i++){
String key = paramBodyKey.get(i);
String value = strs[i];
map.put(key, value);
}
return map;
} public static NewCustInfo getbean(Map<String, String> map){
NewCustInfo newcustinfo = new NewCustInfo();
Class clazz = NewCustInfo.class;
for(String s : map.keySet()){
String key ="set" + s.substring(0, 1).toUpperCase()+s.substring(1, s.length());
try {
Method method = clazz.getMethod(key, String.class);
method.invoke(newcustinfo,map.get(s));
} catch (Exception e) {
e.printStackTrace();
}
} return newcustinfo;
} public static void main(String[] args) {
String str = "||XX|||XX|XX|XX|XX||XX|XX|XXX||||XX|||||XX||XX||XX|XX|XX|XX|XX|XX||XX|XX|";
Map<String, String> map = padBean(str);
System.out.println(getbean(map)); }
}