今天领导让写了一个jackson的工具类,我写的是在静态代码块中获取spring 容器中的jackson,但是最终领导改成了双检锁获取实例,不知道这两种有什么区别,求解
private static ObjectMapper objectMapper = null;
//自己写的
static {
objectMapper = SpringUtil.getBean(ObjectMapper.class);
}
//领导写的
private static ObjectMapper getObjectMapper(){
if (Objects.isNull(objectMapper)){
synchronized (JsonUtils.class){
if (Objects.isNull(objectMapper)){
objectMapper = SpringUtil.getBean(ObjectMapper.class);
}
}
}
return objectMapper;
}