BeanUtils.copyProperties復(fù)制屬性失敗的原因及解決方案
BeanUtils.copyProperties復(fù)制屬性失敗
描述
在JavaE中使用 BeanUtils.copyProperties,把A對(duì)象的name、age等屬性復(fù)制到B對(duì)象中,A與B對(duì)象的類型不同。出現(xiàn)的問(wèn)題是復(fù)制屬性失敗,根本原因是 BeanUtils找不到set、get方法。
import org.springframework.beans.BeanUtils; BeanUtils.copyProperties(one, monitorCount);
解決辦法
1,為復(fù)制對(duì)象的屬性增加set、get方法。比如給name、age屬性增加set、get方法。
2,也可以使用插件生成setter、getter比如:
package com.css.oa.exam.monitor.bean;
import lombok.Data; //使用lombok插件
@Data //使用這個(gè)注解可以生成setter
public class AssignOne{
public String name;
public String age;
}
BeanUtils.copyProperties應(yīng)用的改進(jìn)
在MVC的開(kāi)發(fā)模式中經(jīng)常需要將model與pojo的數(shù)據(jù)綁定,apache和spring的工具包中都有BeanUtils,使用其中的copyProperties方法可以非常方便的進(jìn)行這些工作,但在實(shí)際應(yīng)用中發(fā)現(xiàn),對(duì)于null的處理不太符合個(gè)人的需要,例如在進(jìn)行修改操作中只需要對(duì)model中某一項(xiàng)進(jìn)行修改,那么一般我們?cè)陧?yè)面上只提交model的ID及需要修改項(xiàng)的值,這個(gè)時(shí)候使用BeanUtils.copyProperties會(huì)將其他的null綁定到pojo中去。
為解決這個(gè)問(wèn)題我重寫了部分spring BeanUtils的代碼
如下:
public abstract class BeanUtils extends org.springframework.beans.BeanUtils {
public static void copyProperties(Object source, Object target) throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
Class<?> actualEditable = target.getClass();
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
for (PropertyDescriptor targetPd : targetPds) {
if (targetPd.getWriteMethod() != null) {
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null && sourcePd.getReadMethod() != null) {
try {
Method readMethod = sourcePd.getReadMethod();
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
// 這里判斷以下value是否為空 當(dāng)然這里也能進(jìn)行一些特殊要求的處理 例如綁定時(shí)格式轉(zhuǎn)換等等
if (value != null) {
Method writeMethod = targetPd.getWriteMethod();
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
} catch (Throwable ex) {
throw new FatalBeanException("Could not copy properties from source to target", ex);
}
}
}
}
}
}
apahce的BeanUtils的處理方法類似
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java Swing中的工具欄(JToolBar)和分割面版(JSplitPane)組件使用案例
這篇文章主要介紹了Java Swing中的工具欄(JToolBar)和分割面版(JSplitPane)組件使用案例,本文直接給出代碼實(shí)例和效果截圖,需要的朋友可以參考下2014-10-10
SpringMvc接受請(qǐng)求參數(shù)的幾種情況演示
Springmvc接受請(qǐng)求參數(shù)的幾種介紹,如何接受json請(qǐng)求參數(shù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-07-07
關(guān)于Springboot打成JAR包后讀取外部配置文件的問(wèn)題
這篇文章主要介紹了關(guān)于Springboot打成JAR包后讀取外部配置文件的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
SpringBoot綁定配置文件中變量的四種方式總結(jié)
當(dāng)在Spring Boot中需要綁定配置文件中的變量時(shí),可以使用以下注解:@PropertySourc,@Value,@Environment,@ConfigurationProperties,具體實(shí)現(xiàn)代碼示例文中講解的非常詳細(xì),需要的朋友可以參考下2023-11-11
Java和c語(yǔ)言隨機(jī)數(shù)Random代碼詳細(xì)
這篇文章主要介紹Java和c語(yǔ)言得隨機(jī)數(shù)Random,隨機(jī)數(shù)的用處在生活中比較少見(jiàn),但是用處并不少,比如一些小游戲的制作等等。下面我們就一起來(lái)學(xué)習(xí)這篇關(guān)于Java和c隨機(jī)數(shù)Random得文章吧2021-10-10
springboot之Duration(java.time.Duration)在yml properties中
這篇文章主要介紹了springboot之Duration(java.time.Duration)在yml properties中的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12

