Skip to content

Instantly share code, notes, and snippets.

@upangka
Created January 6, 2020 11:26
Show Gist options
  • Save upangka/c28d3b9cc3e66c5da84e8bb9797201fe to your computer and use it in GitHub Desktop.
Save upangka/c28d3b9cc3e66c5da84e8bb9797201fe to your computer and use it in GitHub Desktop.
SpringUtils 相同属性值赋值,如何属性名称相同,但类型不同,则赋值不成功
import java.lang.reflect.InvocationTargetException;

import org.springframework.beans.BeanUtils;

import lombok.Data;
@Data
class Orig1{
	String id;
}
@Data
class Orig2{
	Integer id;
	
}
@Data
class Dest{
	Integer id;
}



public class TestBeanUtil {
	public static void main(String[] args) throws IllegalAccessException, InvocationTargetException {
		Orig1 orig1 = new Orig1();	orig1.setId("1");
		Orig2 orig2 = new Orig2();	orig2.setId(2);
		Dest dest1 = new Dest();
		Dest dest2 = new Dest();
		BeanUtils.copyProperties(orig1, dest1);
		BeanUtils.copyProperties(orig2, dest2);
		System.out.println(dest1);
		System.out.println(dest2);
	}
}
/**
Dest(id=null)
Dest(id=2)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment