# EasyCopy **Repository Path**: li_ziyun/EasyCopy ## Basic Information - **Project Name**: EasyCopy - **Description**: 对象创建型框架:EasyCopy 1.使用方式简单,编写接口即可。支持方法名映射。 2.支持多级拷贝 例如: Student.name 映射 Teacher.School.name 3.支持深克隆 例如:创建两个完全一样对象,但是内存地址不一样 4.支持运算符操作 例如: stu.setAge( (tea.getAge()+3)*2 ) 5.支持自定义类型转换器 例如: A对象Date属性 转换成Long类型 拷贝到B对象的ID属性 6.默认高精度运算,所有浮点型数据运算结果,保证高精度 7.支持BigDecimal与其他类型直接运算,例如BigDecimal可以与Double类型直接运算 8.支持强制转换,Double转Integer,String转Double,Double转BigDecimal,BigDecimal转Double,Double转String等 9.默认开启自动转换,Integer转Double,Integer转Float,Integer转Long,Byte转Integer等 9.默认整合Spring框架,自动将代理注入到IOC容器中。支持脱离框架单 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 2 - **Created**: 2019-10-06 - **Last Updated**: 2022-11-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # EasyCopy1.0 ``` 1.0的原理: 动态代理 和 反射. 存在问题: 性能低 2.0的原理: 编译时期直接生成字节码,无性能损失无反射. 2.0地址: https://gitee.com/li_ziyun/easy-copy2.0 ``` 推荐使用2.0: https://gitee.com/li_ziyun/easy-copy2.0 **使用场景** ``` 对象的属性相互拷贝时 ``` **特性** ``` 1.使用方式简单,编写接口即可。支持方法名映射。 2.支持多级拷贝 例如: Student.name 映射 Teacher.School.name 3.支持深克隆 例如:创建两个完全一样对象,但是内存地址不一样 4.支持运算符操作 例如: stu.setAge( (tea.getAge()+3)*2 ) 5.支持自定义类型转换器 例如: A对象Date属性 转换成Long类型 拷贝到B对象的ID属性 6.默认高精度运算,所有浮点型数据运算结果,保证高精度 7.支持BigDecimal与其他类型直接运算,例如BigDecimal可以与Double类型直接运算 8.支持强制转换,Double转Integer,String转Double,Double转BigDecimal,BigDecimal转Double,Double转String等 9.默认开启自动转换,Integer转Double,Integer转Float,Integer转Long,Byte转Integer等 9.默认整合Spring框架,自动将代理注入到IOC容器中。支持脱离框架单独使用。 10.封装异常类型,常见错误可以快速定位。 ``` **使用方式一:注解映射** ``` 需求: 创建新Student对象,Student.name 是 name+(Teacher.age+Teacher.school.id)*1.1+"测试+Demo"+Teacher.age+Teacher.school.id 特点: 复杂的运算表达式都能够被准确解析,运算优先级,每个+号的不同含义 特性: 2,4 @EasyCopy public interface TestParams{ @ParamMaps(value = { @ParamMap(targetProperty = "name", resourceProperty = "arg1+(arg0.age+arg0.school.id)*1.1+'测试*Demo'+arg0.age+arg0.school.id") }) public Student fun6(Teacher teacher,String name); } =================================================================== 需求: 两个已经创建的对象。Teacher.school 深克隆 Student.school 3 @EasyCopy public interface TestParams{ @ParamMaps(value={ @ParamMap(targetProperty="arg0.school",resourceProperty="arg1.school",CLONE_TYPE = CloneType.DeepClone) },eagerInstance = true) public Student fun(Student student,Teacher teacher); } =================================================================== 需求:创建新对象。Student.id 映射 Teacher.date 注意:配置类型转换器TYPE_TRANSFORM属性,TypeTransform接口实现类的字节码 5 @EasyCopy public interface TestParams{ @ParamMaps(value={ @ParamMap(targetProperty="id",resourceProperty="arg0.date",TYPE_TRANSFORM = TypeTransform接口实现类的字节码) }) public Student fun(Teacher teacher); } =============================================================== 需求:创建新Student对象,Student.a(BigDecimal类型) 映射 Teacher.money(BigDecimal类型)*money(Double类型) 特点: 必须开启openForceCast=true,因为Teacher.money*money的结果是double类型,需要强制转换BigDecimal 7,8 @EasyCopy public interface TestParams{ @ParamMaps(value={ @ParamMap(targetProperty="a",resourceProperty="arg0.money*arg1",openForceCast=true) }) public Student fun(Teacher teacher,Double money); } ================================================================== 需求: 高精度运算,保证浮点型数据运算结果没有精度的丢失。 特点:浮点型运算,框架内部默认情况保证精度。 @EasyCopy public interface TestParams{ @ParamMaps(value={ @ParamMap(targetProperty="salary",resourceProperty="arg0.salary*arg1") }) public Student fun(Teacher teacher,Double money); } ``` **使用方式二:方法名映射** ``` 需求:创建一个新Student对象,age属性和name属性,来自Teacher对象 普通方式: Student stu = new Student(); stu.setAge(tea.getAge()); stu.setName(tea.getName()); EasyCopy方式: @EasyCopy public interface CreateStudent{ public Student ageByAgeAndNameByName(Teacher teacher); } ==================================================================== 需求:两个已经创建的对象,拷贝属性 普通方式: stu.setAge(tea.getAge()); stu.setName(tea.getName()); EasyCopy方式: @EasyCopy public interface CreateStudent{ public Student ageByAgeAndNameByName(Student student,Teacher teacher); } 注意事项: 方法名映射,默认采用浅克隆。 不能配置类型转换器 ``` **使用方式三:快速克隆** ``` 需求: 快速克隆一个一模一样的对象,多级深克隆 EasyCopy方式: @EasyCopy public interface TestClone { @Clone public Student clone(Student student); } 注意事项:Student对象必须实现序列化接口 ``` **启动方式** ``` 1.整合Spring,自动将代理对象,注册到IOC容器中 2.脱离Spring,独立使用 ``` **Spring方式** ``` @Configuration public class SpringApplication { @Bean public RegistryProxyTpSpringApplicationContext easyCopyApplication(){ return new RegistryProxyTpSpringApplicationContext("com.copy"); } } ``` ``` @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration( classes =SpringApplication.class ) public class TestSpring { @Autowired(required = true) private TestNameAuto nameAuto; @Autowired private TestParamsAnnotation paramsAnnotation; @Autowired private TestClone clone; } ``` **独立使用** ``` public class TestOther { private GenericCreatePorxyHandler application; private TestNameAuto nameAuto; private TestParamsAnnotation paramsAnnotation; private TestClone clone; public void init(){ application = GenericCreatePorxyHandler.getInstance(); nameAuto = application.getProxyBean(TestNameAuto.class); paramsAnnotation = application.getProxyBean(TestParamsAnnotation.class); clone = application.getProxyBean(TestClone.class); } } ```