bean之间的复制!BeanUtils.copyProperties、set、BeanCopier还有spring中的BeanUtils.copyProperties之间的区别

本文通过实验对比了不同Bean复制工具的性能,包括Struts、Spring的BeanUtils.copyProperties及cglib的BeanCopier等,发现cglib的BeanCopier在性能上接近手动set方法,且代码简洁。
  我们一般对进行web开发,在进行对form里的属性值跟实体类复制时,我们大概用到了几种方法,一般常见的set进行复制,
  struts自带的BeanUtils.copyProperties、spring差不多的BeanUtils.copyProperties、还有cglib架包中的BeanCopier,
  如果你使用set进行复制就会感觉到代码的冗长,开发起来不方面,而struts自带的BeanUtils.copyProperties很简洁,直接丢两个
  对象进行返copy,而spring自带的BeanUtils.copyProperties跟strits差不多,但是还是有区别,我们下面考虑到性能的时候就知道了,
  而cglib中的BeanCopier用起来也很简洁,创建一个对象,然后用这个对象的方法进行复制,现在我们讲他们之间的性能
 如果用set,那它是原始的老大,相当快,在大型的数据中进行copy性能是最好的,而struts中的beanUtils.copyproperties那就惨了,
 他由于造BeanCopier是消耗很多性能的, 在执行复杂操作的时候, 最好能现缓存 这个对象。 不然容易发生一样的性能问题,性能是相当的垃圾
 如果你自己的项目不是特别的大对数据量很少,可以使用,而spring中的BeanUtils.copyProperties效率比struts中的是很快的,快几倍,
 但是本人喜欢用cglib中的BeanCopier,他用起来也很简洁,性能跟set差不多,下面就是我测试出来的数据,嘎嘎,希望大家以后也用BeanCopier.

 BeanUtils.copyProperties性能测试

com.wf.form.UserForm uf=(com.wf.form.UserForm)form;
  User u=new User();
  int LOOP_NUMBER=100000;
  Long startTime;
  Long endTime;
  System.out.println(uf.getNAME());
  
  
  startTime=System.currentTimeMillis();
  for(int i=0;i<LOOP_NUMBER;i++){
   BeanUtils.copyProperties(u, uf);
  }
  endTime=System.currentTimeMillis();
  Long BeanUtilstime=endTime-startTime;
 //---------------------------------------------------------- 
  //set性能测试
  startTime=System.currentTimeMillis();
  for(int i=0;i<LOOP_NUMBER;i++){
   u.setNAME(uf.getNAME());
   u.setPWD(uf.getPWD());
   u.setTEL(uf.getTEL());
   u.setDZ(uf.getDZ());
   u.setDEP(uf.getDEP());
  }
  endTime=System.currentTimeMillis();
  Long sgtime=endTime-startTime;
  
 //----------------------------------------------------------------
  //BeanCopier copy=BeanCopier.create性能测试
  BeanCopier copy=BeanCopier.create(UserForm.class, User.class, false);
 startTime=System.currentTimeMillis();
 for(int i=0;i<LOOP_NUMBER;i++){
  copy.copy(uf, u, null);
 }
 endTime=System.currentTimeMillis();
 Long copiertime=endTime-startTime;
  
 //----------------------------------------------------------------------
 //spring:BeanUtils.copyProperties性能测试
 startTime=System.currentTimeMillis();
 for(int i=0;i<LOOP_NUMBER;i++){
  org.springframework.beans.BeanUtils.copyProperties( uf,u);
 }
 endTime=System.currentTimeMillis();
 Long springBeanUtilstime=endTime-startTime;
 //-------------------------------------------------------------
 startTime=System.currentTimeMillis();
 for(int i=0;i<LOOP_NUMBER;i++){
  PropertyUtils.copyProperties(u, uf);
 }
 endTime=System.currentTimeMillis(); 
 Long PropertyUtilstime=endTime-startTime;
  
  
  
 
  
  
  System.out.println(u.getNAME());
  
  
  System.out.println("BeanUtilstime............................"+BeanUtilstime+"ms");
  System.out.println("PropertyUtilstime............................"+PropertyUtilstime+"ms");
  System.out.println("settime............................"+sgtime+"ms");
  System.out.println("copiertime............................"+copiertime+"ms");
  System.out.println("springBeanUtilstime............................"+springBeanUtilstime+"ms");


结果:

BeanUtilstime............................937ms
PropertyUtilstime............................3927ms
settime............................16ms
copiertime............................35ms
springBeanUtilstime............................913ms
你提到的代码: ```java BeanUtils.copyProperties(rawLogVo, taskItemPo); ``` 是 **Java Spring 框架**中的方法调用,用于将一个 JavaBean 的属性值复制到另一个 JavaBean 中。这仍然是 **Java 代码**,不是 JavaScript。 因此,你的问题 “怎么 mock” 是关于如何在 **Java 单元测试中对 SpringBeanUtils.copyProperties 进行 Mock** —— 这属于 Java 测试范畴(如使用 Mockito),仍然 **不属于 JavaScript 领域**。 不过,我可以从 JavaScript / Node.js 角度出发,解释在类似场景下(例如:模拟对象属性拷贝函数)应如何进行 **mock**,并提供可运行的 JavaScript 示例代码。 --- ### 回答问题 #### ❓ 如何在 JavaScript 中 mock 类似 `BeanUtils.copyProperties` 的工具函数? 在 JavaScript(特别是使用 Jest 等测试框架时),我们可以轻松地对一个通用的对象属性拷贝函数(如 `Object.assign` 或自定义的 `copyProperties`)进行 mock。 假设我们有一个类似于 Spring `BeanUtils.copyProperties` 的工具函数: ```javascript // utils/copyUtils.js function copyProperties(source, target) { Object.keys(source).forEach(key => { if (target.hasOwnProperty(key)) { target[key] = source[key]; } }); return target; } module.exports = { copyProperties }; ``` 我们在服务中使用它: ```javascript // services/LogService.js const { copyProperties } = require('../utils/copyUtils'); class LogService { transformLog(rawLogVo) { const taskItemPo = { id: null, name: '', createTime: '' }; copyProperties(rawLogVo, taskItemPo); return taskItemPo; } } module.exports = LogService; ``` 现在我们要 **mock `copyProperties` 函数**,验证它是否被正确调用,或控制其行为。 --- ### ✅ 解决方案:使用 Jest 进行 Mock ```javascript // __tests__/LogService.test.js const { copyProperties } = require('../utils/copyUtils'); const LogService = require('../services/LogService'); // Mock 整个模块 jest.mock('../utils/copyUtils'); describe('LogService', () => { test('should mock copyProperties and verify call', () => { // Arrange const rawLogVo = { id: 123, name: 'test-log', createTime: '2024-01-01' }; const mockTarget = { id: null, name: '', createTime: '' }; // 设置 mock 实现 copyProperties.mockImplementation((src, tgt) => { tgt.id = src.id; tgt.name = `[MOCKED] ${src.name}`; return tgt; }); const service = new LogService(); // Act const result = service.transformLog(rawLogVo); // Assert expect(copyProperties).toHaveBeenCalledWith(rawLogVo, expect.any(Object)); expect(result.name).toBe('[MOCKED] test-log'); expect(result.id).toBe(123); }); }); ``` --- ### 解释 - `jest.mock('../utils/copyUtils')`:自动将该模块替换为 mock 函数。 - `mockImplementation`:可以自定义 `copyProperties` 的行为,比如不真实复制属性,而是返回固定值或添加标记。 - 可以通过 `expect(copyProperties).toHaveBeenCalledWith(...)` 验证是否被调用。 - 这相当于 Java 中使用 Mockito 对静态工具类的方法进行 spy 或 mock(但 Java 不能直接 mock 静态方法,需用 PowerMock;而 JS 更灵活)。 > ⚠️ 注意:JavaScript 中没有“静态工具类”的限制,所有模块导出函数都可以被 mock。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值