2、其它,看具体业务需求吧,哪些字段不想被序列化;
PS,记得之前看HashMap源码的时候,发现有个字段是用transient修饰的,我觉得还是有道理的,确实没必要对这个modCount字段进行序列化,因为没有意义,modCount主要用于判断HashMap是否被修改(像put、remove操作的时候,modCount都会自增),对于这种变量,一开始可以为任何值,0当然也是可以(new出来、反序列化出来、或者克隆clone出来的时候都是为0的),没必要持久化其值。
/**
*The number of times this HashMap has been structurally modified
*Structural modifications arethose that changethenumber of mappings in
*theHashMap or otherwise modify its internal structure (e.g.,
*rehash). This field is used to make iterators on Collection-views of
*theHashMap fail-fast. (See ConcurrentModificationException).
*/
transient intmodCount;
最后,为什么要不被序列化呢,主要是为了节省存储空间,其它的感觉没啥好处,可能还有坏处(有些字段可能需要重新计算,初始化什么的),总的来说,利大于弊。
举个例子
仅仅是示例,具体使用请根据实际情况:
package com.ecej.esmart.workform.queue;
import com.ecej.esmart.core.annotation.Log;
import com.ecej.esmart.workform.dao.EsmartBaseDao;
import com.ecej.nove.utils.common.BaseUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 删除周期订单虚拟档期表
*/
@Component
@RabbitListener(queues = "${esmart.del.cycle.order.schedule}")
publicclassReceiverDelCycleOrderSchedule {
privatestaticfinalLogger LOGGER = LoggerFactory.getLogger(ReceiverDelCycleOrderSchedule.class);
@Resource
privateEsmartBaseDao esmartBaseDao;
@Log("删除周期订单虚拟档期表")
@RabbitHandler
publicvoid delCycleOrderSchedule(String parentOrderNo) {
try {
LOGGER.info("监听到的消息:{}", parentOrderNo);
if (StringUtils.isNotEmpty(parentOrderNo)) {
Map objectMap = newHashMap<>();
objectMap.put("parentOrderNo", parentOrderNo);
esmartBaseDao.delete(BaseUtils.makeClazzPath(com.ecej.esmart.workform.ecejservice.po.WorkOrderPo.class, "delCycleOrderSchedule"), objectMap);
}
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
}
结果打印(达到目的,节省存储空间,成功恢复成原始对象):
1.原始对象
width : 3 height : 4 area : 12
2.反序列化后的对象
width : 3 height : 4 area : null
3.恢复成原始对象
width : 3 height : 4 area : 12
1.原始对象
width : 3 height : 4 area : 12
2.反序列化后的对象
width : 3 height : 4 area : null
3.恢复成原始对象
width : 3 height : 4 area : 12
来源:https://blog.csdn.net/
u011955252/article/details/78947554
来源:https://blog.csdn.net/
u011955252/article/details/78947554