实体
public class ConsultantEntity {
private String userName;
private String phone;
private String title;
private String content;
private String orgName;
private String orgCode;
private String regionCode;
private String regionName;
private String userId;
private String socialCreditCode;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getOrgName() {
return orgName;
}
public void setOrgName(String orgName) {
this.orgName = orgName;
}
public String getOrgCode() {
return orgCode;
}
public void setOrgCode(String orgCode) {
this.orgCode = orgCode;
}
public String getRegionCode() {
return regionCode;
}
public void setRegionCode(String regionCode) {
this.regionCode = regionCode;
}
public String getRegionName() {
return regionName;
}
public void setRegionName(String regionName) {
this.regionName = regionName;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getSocialCreditCode() {
return socialCreditCode;
}
public void setSocialCreditCode(String socialCreditCode) {
this.socialCreditCode = socialCreditCode;
}
}
配置文件 核心代码
for (DictionaryItem dictionaryItem : dictionaryItems) {
Method declaredMethod = consultantEntity.getClass().getDeclaredMethod(dictionaryItem.getName(), null);
Object result = declaredMethod.invoke(consultantEntity, null);
if (result == null){
throw new Exception(dictionaryItem.getValue());
}
}
1、先使用getDeclaredMetho()获取实例的方法 第一个参数为实例方法名 第二参数是方法的参数类型,是一个数组,无参方法直接传null
2、拿到方法,执行方法 使用invoke()方法执行 第一个参数是实例, 第二参数是执行方法的参数
举个例子: dictionaryItem.getName()从配置中读取方法名getUserId,实例的**getUserId()是无参方法,所以getDeclaredMetho()**返回了实例的
public String getUserId() {
return userId;
}
然后**declaredMethod.invoke(consultantEntity, null);执行getUserId()**方法
|