如何使用BeanUtils.copyProperties?

我试图将属性从一个bean复制到另一个bean。 这是两个bean的签名:

SearchContent

 public class SearchContent implements Serializable { private static final long serialVersionUID = -4500094586165758427L; private Integer id; private String docName; private String docType; private String docTitle; private String docAuthor; private String securityGroup; private String docAccount; private Integer revLabel; private String profile; private LabelValueBean workflowStage; private Date createDate; private Date inDate; private String originalName; private String format; private String extension; private Long fileSize; private String author; private LabelValueBean entity; private LabelValueBean brand; private LabelValueBean product; private LabelValueBean collection; private LabelValueBean subCollection; private String description; private LabelValueBean program; private String vintage; private String model; private String restrictedZone; private LabelValueBean event; private LabelValueBean language; private String geographicLocation; private String watermark; private Integer pageNumber; private String summary; private String agentName; private String commissionedByName; private String commissionedByDepartment; private LabelValueBean bestOf; private String mediaLocation; private LabelValueBean fullRights; private LabelValueBean mediaUsage; private Date rightsEndDate; private String geographicRights; private LabelValueBean evinConformity; private String contactReference; private LabelValueBean publicationScope; private String rightsComment; /** * Constructor SearchContent * @author TapasB * @since 15-Oct-2013 - 5:45:55 pm * @version DAM 1.0 */ public SearchContent() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getDocName() { return docName; } public void setDocName(String docName) { this.docName = docName; } public String getDocType() { return docType; } public void setDocType(String docType) { this.docType = docType; } public String getDocTitle() { return docTitle; } public void setDocTitle(String docTitle) { this.docTitle = docTitle; } public String getDocAuthor() { return docAuthor; } public void setDocAuthor(String docAuthor) { this.docAuthor = docAuthor; } public String getSecurityGroup() { return securityGroup; } public void setSecurityGroup(String securityGroup) { this.securityGroup = securityGroup; } public String getDocAccount() { return docAccount; } public void setDocAccount(String docAccount) { this.docAccount = docAccount; } public Integer getRevLabel() { return revLabel; } public void setRevLabel(Integer revLabel) { this.revLabel = revLabel; } public String getProfile() { return profile; } public void setProfile(String profile) { this.profile = profile; } public LabelValueBean getWorkflowStage() { return workflowStage; } public void setWorkflowStage(LabelValueBean workflowStage) { this.workflowStage = workflowStage; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public Date getInDate() { return inDate; } public void setInDate(Date inDate) { this.inDate = inDate; } public String getOriginalName() { return originalName; } public void setOriginalName(String originalName) { this.originalName = originalName; } public String getFormat() { return format; } public void setFormat(String format) { this.format = format; } public String getExtension() { return extension; } public void setExtension(String extension) { this.extension = extension; } public Long getFileSize() { return fileSize; } public void setFileSize(Long fileSize) { this.fileSize = fileSize; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public LabelValueBean getEntity() { return entity; } public void setEntity(LabelValueBean entity) { this.entity = entity; } public LabelValueBean getBrand() { return brand; } public void setBrand(LabelValueBean brand) { this.brand = brand; } public LabelValueBean getProduct() { return product; } public void setProduct(LabelValueBean product) { this.product = product; } public LabelValueBean getCollection() { return collection; } public void setCollection(LabelValueBean collection) { this.collection = collection; } public LabelValueBean getSubCollection() { return subCollection; } public void setSubCollection(LabelValueBean subCollection) { this.subCollection = subCollection; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public LabelValueBean getProgram() { return program; } public void setProgram(LabelValueBean program) { this.program = program; } public String getVintage() { return vintage; } public void setVintage(String vintage) { this.vintage = vintage; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public String getRestrictedZone() { return restrictedZone; } public void setRestrictedZone(String restrictedZone) { this.restrictedZone = restrictedZone; } public LabelValueBean getEvent() { return event; } public void setEvent(LabelValueBean event) { this.event = event; } public LabelValueBean getLanguage() { return language; } public void setLanguage(LabelValueBean language) { this.language = language; } public String getGeographicLocation() { return geographicLocation; } public void setGeographicLocation(String geographicLocation) { this.geographicLocation = geographicLocation; } public String getWatermark() { return watermark; } public void setWatermark(String watermark) { this.watermark = watermark; } public Integer getPageNumber() { return pageNumber; } public void setPageNumber(Integer pageNumber) { this.pageNumber = pageNumber; } public String getSummary() { return summary; } public void setSummary(String summary) { this.summary = summary; } public String getAgentName() { return agentName; } public void setAgentName(String agentName) { this.agentName = agentName; } public String getCommissionedByName() { return commissionedByName; } public void setCommissionedByName(String commissionedByName) { this.commissionedByName = commissionedByName; } public String getCommissionedByDepartment() { return commissionedByDepartment; } public void setCommissionedByDepartment(String commissionedByDepartment) { this.commissionedByDepartment = commissionedByDepartment; } public LabelValueBean getBestOf() { return bestOf; } public void setBestOf(LabelValueBean bestOf) { this.bestOf = bestOf; } public String getMediaLocation() { return mediaLocation; } public void setMediaLocation(String mediaLocation) { this.mediaLocation = mediaLocation; } public LabelValueBean getFullRights() { return fullRights; } public void setFullRights(LabelValueBean fullRights) { this.fullRights = fullRights; } public LabelValueBean getMediaUsage() { return mediaUsage; } public void setMediaUsage(LabelValueBean mediaUsage) { this.mediaUsage = mediaUsage; } public Date getRightsEndDate() { return rightsEndDate; } public void setRightsEndDate(Date rightsEndDate) { this.rightsEndDate = rightsEndDate; } public String getGeographicRights() { return geographicRights; } public void setGeographicRights(String geographicRights) { this.geographicRights = geographicRights; } public LabelValueBean getEvinConformity() { return evinConformity; } public void setEvinConformity(LabelValueBean evinConformity) { this.evinConformity = evinConformity; } public String getContactReference() { return contactReference; } public void setContactReference(String contactReference) { this.contactReference = contactReference; } public LabelValueBean getPublicationScope() { return publicationScope; } public void setPublicationScope(LabelValueBean publicationScope) { this.publicationScope = publicationScope; } public String getRightsComment() { return rightsComment; } public void setRightsComment(String rightsComment) { this.rightsComment = rightsComment; } } 

Content

 public class Content implements Serializable { private static final long serialVersionUID = 2999449587418137835L; private Boolean selected; private Boolean renditionInfoFetched; private String searchPageImageRendition; private String detailPageImageRendition; private String videoRendition; private Integer id; private String docName; private String docType; private String docTitle; private String docAuthor; private String securityGroup; private String docAccount; private Integer revLabel; private String profile; private Date createDate; private Date inDate; private String originalName; private String format; private String extension; private Long fileSize; private String author; private LabelValueBean entity; private LabelValueBean brand; private LabelValueBean product; private LabelValueBean collection; private LabelValueBean subCollection; private String description; private LabelValueBean program; private String vintage; private String model; private String restrictedZone; private LabelValueBean event; private LabelValueBean language; private String geographicLocation; private String watermark; private Integer pageNumber; private String summary; private String agentName; private String commissionedByName; private String commissionedByDepartment; private LabelValueBean bestOf; private String mediaLocation; private LabelValueBean fullRights; private LabelValueBean mediaUsage; private Date rightsEndDate; private String geographicRights; private LabelValueBean evinConformity; private String contactReference; private LabelValueBean publicationScope; private String rightsComment; public Boolean getSelected() { return selected; } public void setSelected(Boolean selected) { this.selected = selected; } public Boolean getRenditionInfoFetched() { return renditionInfoFetched; } public void setRenditionInfoFetched(Boolean renditionInfoFetched) { this.renditionInfoFetched = renditionInfoFetched; } public String getSearchPageImageRendition() { return searchPageImageRendition; } public void setSearchPageImageRendition(String searchPageImageRendition) { this.searchPageImageRendition = searchPageImageRendition; } public String getDetailPageImageRendition() { return detailPageImageRendition; } public void setDetailPageImageRendition(String detailPageImageRendition) { this.detailPageImageRendition = detailPageImageRendition; } public String getVideoRendition() { return videoRendition; } public void setVideoRendition(String videoRendition) { this.videoRendition = videoRendition; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getDocName() { return docName; } public void setDocName(String docName) { this.docName = docName; } public String getDocType() { return docType; } public void setDocType(String docType) { this.docType = docType; } public String getDocTitle() { return docTitle; } public void setDocTitle(String docTitle) { this.docTitle = docTitle; } public String getDocAuthor() { return docAuthor; } public void setDocAuthor(String docAuthor) { this.docAuthor = docAuthor; } public String getSecurityGroup() { return securityGroup; } public void setSecurityGroup(String securityGroup) { this.securityGroup = securityGroup; } public String getDocAccount() { return docAccount; } public void setDocAccount(String docAccount) { this.docAccount = docAccount; } public Integer getRevLabel() { return revLabel; } public void setRevLabel(Integer revLabel) { this.revLabel = revLabel; } public String getProfile() { return profile; } public void setProfile(String profile) { this.profile = profile; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public Date getInDate() { return inDate; } public void setInDate(Date inDate) { this.inDate = inDate; } public String getOriginalName() { return originalName; } public void setOriginalName(String originalName) { this.originalName = originalName; } public String getFormat() { return format; } public void setFormat(String format) { this.format = format; } public String getExtension() { return extension; } public void setExtension(String extension) { this.extension = extension; } public Long getFileSize() { return fileSize; } public void setFileSize(Long fileSize) { this.fileSize = fileSize; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public LabelValueBean getEntity() { return entity; } public void setEntity(LabelValueBean entity) { this.entity = entity; } public LabelValueBean getBrand() { return brand; } public void setBrand(LabelValueBean brand) { this.brand = brand; } public LabelValueBean getProduct() { return product; } public void setProduct(LabelValueBean product) { this.product = product; } public LabelValueBean getCollection() { return collection; } public void setCollection(LabelValueBean collection) { this.collection = collection; } public LabelValueBean getSubCollection() { return subCollection; } public void setSubCollection(LabelValueBean subCollection) { this.subCollection = subCollection; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public LabelValueBean getProgram() { return program; } public void setProgram(LabelValueBean program) { this.program = program; } public String getVintage() { return vintage; } public void setVintage(String vintage) { this.vintage = vintage; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public String getRestrictedZone() { return restrictedZone; } public void setRestrictedZone(String restrictedZone) { this.restrictedZone = restrictedZone; } public LabelValueBean getEvent() { return event; } public void setEvent(LabelValueBean event) { this.event = event; } public LabelValueBean getLanguage() { return language; } public void setLanguage(LabelValueBean language) { this.language = language; } public String getGeographicLocation() { return geographicLocation; } public void setGeographicLocation(String geographicLocation) { this.geographicLocation = geographicLocation; } public String getWatermark() { return watermark; } public void setWatermark(String watermark) { this.watermark = watermark; } public Integer getPageNumber() { return pageNumber; } public void setPageNumber(Integer pageNumber) { this.pageNumber = pageNumber; } public String getSummary() { return summary; } public void setSummary(String summary) { this.summary = summary; } public String getAgentName() { return agentName; } public void setAgentName(String agentName) { this.agentName = agentName; } public String getCommissionedByName() { return commissionedByName; } public void setCommissionedByName(String commissionedByName) { this.commissionedByName = commissionedByName; } public String getCommissionedByDepartment() { return commissionedByDepartment; } public void setCommissionedByDepartment(String commissionedByDepartment) { this.commissionedByDepartment = commissionedByDepartment; } public LabelValueBean getBestOf() { return bestOf; } public void setBestOf(LabelValueBean bestOf) { this.bestOf = bestOf; } public String getMediaLocation() { return mediaLocation; } public void setMediaLocation(String mediaLocation) { this.mediaLocation = mediaLocation; } public LabelValueBean getFullRights() { return fullRights; } public void setFullRights(LabelValueBean fullRights) { this.fullRights = fullRights; } public LabelValueBean getMediaUsage() { return mediaUsage; } public void setMediaUsage(LabelValueBean mediaUsage) { this.mediaUsage = mediaUsage; } public Date getRightsEndDate() { return rightsEndDate; } public void setRightsEndDate(Date rightsEndDate) { this.rightsEndDate = rightsEndDate; } public String getGeographicRights() { return geographicRights; } public void setGeographicRights(String geographicRights) { this.geographicRights = geographicRights; } public LabelValueBean getEvinConformity() { return evinConformity; } public void setEvinConformity(LabelValueBean evinConformity) { this.evinConformity = evinConformity; } public String getContactReference() { return contactReference; } public void setContactReference(String contactReference) { this.contactReference = contactReference; } public LabelValueBean getPublicationScope() { return publicationScope; } public void setPublicationScope(LabelValueBean publicationScope) { this.publicationScope = publicationScope; } public String getRightsComment() { return rightsComment; } public void setRightsComment(String rightsComment) { this.rightsComment = rightsComment; } } 

我试图将SearchContent属性复制到Content

 Content content = new Content(); Converter converter = new DateConverter(null); BeanUtilsBean beanUtilsBean = BeanUtilsBean.getInstance(); beanUtilsBean.getConvertUtils().register(converter, Date.class); BeanUtils.copyProperties(searchContent, content); System.out.println(searchContent); System.out.println(content); 

Sysout正在打印:

 com.mhis.dam.service.search.bean.SearchContent[id=52906,docName=MHIS043570,docType=Images,docTitle=preview_1_8,docAuthor=sysadmin,securityGroup=Internal,docAccount=WF/017/DAM/000,revLabel=1,profile=DAMMedia,workflowStage=com.mhis.dam.generic.bean.LabelValueBean[label=Published,value=published],createDate=Fri Oct 18 15:30:35 IST 2013,inDate=Fri Oct 18 15:30:35 IST 2013,originalName=Vintage 2004 Gift Box & Bottle Black - hires.jpg,format=image/jpeg,extension=jpg,fileSize=2106898,author=Arjan,entity=com.mhis.dam.generic.bean.LabelValueBean[label=Dom Perignon,value=WF/017/DAM],brand=com.mhis.dam.generic.bean.LabelValueBean[label=Dom Perignon,value=WF/017/DAM/001],product=com.mhis.dam.generic.bean.LabelValueBean[label=Blanc,value=17_1_blanc],collection=com.mhis.dam.generic.bean.LabelValueBean[label=Pack shot,value=pack_shot_dp],subCollection=com.mhis.dam.generic.bean.LabelValueBean[label=Bottle shot,value=ps_bottle_dp],description=preview_1,program=com.mhis.dam.generic.bean.LabelValueBean[label=,value=],vintage=,model=,restrictedZone=,event=com.mhis.dam.generic.bean.LabelValueBean[label=,value=],language=com.mhis.dam.generic.bean.LabelValueBean[label=,value=],geographicLocation=,watermark=,pageNumber=0,summary=,agentName=,commissionedByName=Nicolas,commissionedByDepartment=,bestOf=com.mhis.dam.generic.bean.LabelValueBean[label=,value=0],mediaLocation=,fullRights=com.mhis.dam.generic.bean.LabelValueBean[label=,value=1],mediaUsage=com.mhis.dam.generic.bean.LabelValueBean[label=,value=],rightsEndDate=,geographicRights=,evinConformity=com.mhis.dam.generic.bean.LabelValueBean[label=,value=2],contactReference=,publicationScope=com.mhis.dam.generic.bean.LabelValueBean[label=Full public usage,value=fullPublic],rightsComment=] com.mhis.dam.web.model.Content[selected=,renditionInfoFetched=,searchPageImageRendition=,detailPageImageRendition=,videoRendition=,id=,docName=,docType=,docTitle=,docAuthor=,securityGroup=,docAccount=,revLabel=,profile=,createDate=,inDate=,originalName=,format=,extension=,fileSize=,author=,entity=,brand=,product=,collection=,subCollection=,description=,program=,vintage=,model=,restrictedZone=,event=,language=,geographicLocation=,watermark=,pageNumber=,summary=,agentName=,commissionedByName=,commissionedByDepartment=,bestOf=,mediaLocation=,fullRights=,mediaUsage=,rightsEndDate=,geographicRights=,evinConformity=,contactReference=,publicationScope=,rightsComment=] 

显然,为Content类的selectedrenditionInfoFetched字段设置空值,因为它们不存在于SearchContent但您可以看到Content所有其他属性都为null。 我无法找到我做错了什么!

任何指针都会非常有用。

如果要从searchContent复制到内容,则代码应如下所示

 BeanUtils.copyProperties(content, searchContent); 

您需要在代码中反转上述参数。

来自API,

public static void copyProperties(Object dest,Object orig)抛出IllegalAccessException,InvocationTargetException)

参数:dest – 其属性已修改的目标bean orig – 检索其属性的Origin bean

http://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/BeanUtils.html#copyProperties(java.lang.Object,java.lang.Object

Java中有两个BeanUtils.copyProperties(parameter1,parameter2)。

一个是

org.apache.commons.beanutils.BeanUtils.copyProperties( Object dest,Object orig

另一个是

org.springframework.beans.BeanUtils.copyProperties( 对象源,对象目标

注意参数的相反位置。