java中在使用copyProperties方法时,源对象比目标对象的属性名多个前缀,如何定义一个属性名转换器,代码要简洁优化,且包括需要导入的库和依赖语句,且用springboot2
时间: 2024-03-22 12:41:41 浏览: 26
在使用copyProperties方法时,可以定义一个属性名转换器来解决源对象比目标对象的属性名多个前缀的问题。下面是一个示例代码,其中使用了spring-beans和spring-context依赖:
```java
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.PropertyAccessor;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.SimpleTypeConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@Configuration
public class PropertyNameConverter {
@Bean
public GenericConverter propertyNameConverter() {
return new ConditionalGenericConverter() {
private final SimpleTypeConverter typeConverter = new SimpleTypeConverter();
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(String.class, String.class));
}
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
}
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return null;
}
String propertyName = source.toString();
if (StringUtils.isEmpty(propertyName)) {
return propertyName;
}
// 定义前缀
String prefix = "src_";
// 如果属性名以前缀开头,去掉前缀
if (propertyName.startsWith(prefix)) {
propertyName = propertyName.substring(prefix.length());
}
// 转换为目标属性名
String targetPropertyName = "dest_" + propertyName;
return targetPropertyName;
}
};
}
}
@Component
public class Demo {
private String dest_name;
private String dest_age;
private String src_name;
private String src_age;
public static void main(String[] args) {
Demo demo = new Demo();
demo.setSrc_name("Tom");
demo.setSrc_age("18");
BeanWrapper src = new BeanWrapperImpl(demo);
BeanWrapper dest = new BeanWrapperImpl(new Demo());
dest.addPropertyChangeListener(new PropertyNameConverter());
BeanUtils.copyProperties(demo, dest.getWrappedInstance(), getNullPropertyNames(src.getWrappedInstance()));
System.out.println(demo.getDest_name()); // 输出:Tom
System.out.println(demo.getDest_age()); // 输出:18
}
/**
* 获取为空的属性名
*/
private static String[] getNullPropertyNames(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
Set<String> emptyNames = new HashSet<>();
for (Field field : source.getClass().getDeclaredFields()) {
String propertyName = field.getName();
try {
PropertyAccessor accessor = PropertyAccessorFactory.forDirectFieldAccess(source);
Object srcValue = accessor.getPropertyValue(propertyName);
if (srcValue == null) {
emptyNames.add(propertyName);
}
} catch (RuntimeException ex) {
// ignore
}
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
public String getDest_name() {
return dest_name;
}
public void setDest_name(String dest_name) {
this.dest_name = dest_name;
}
public String getDest_age() {
return dest_age;
}
public void setDest_age(String dest_age) {
this.dest_age = dest_age;
}
public String getSrc_name() {
return src_name;
}
public void setSrc_name(String src_name) {
this.src_name = src_name;
}
public String getSrc_age() {
return src_age;
}
public void setSrc_age(String src_age) {
this.src_age = src_age;
}
}
```
在上面的代码中,我们定义了一个属性名转换器propertyNameConverter,它可以将源对象中的属性名转换为目标对象中的属性名。在main方法中,我们首先创建了一个Demo对象,并给它的src_name和src_age属性赋值。然后,我们使用BeanUtils.copyProperties方法将Demo对象的属性值拷贝到一个新的Demo对象中,在拷贝过程中,我们使用了propertyNameConverter来转换属性名。最后,我们输出了新Demo对象的dest_name和dest_age属性值。
需要注意的是,在使用copyProperties方法时,我们还需要定义一个getNullPropertyNames方法来获取为空的属性名,以防止copyProperties方法将空值覆盖目标对象的非空属性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)