在Serializable Java类中使用Logger的正确方法是什么?

编程入门 行业动态 更新时间:2024-10-11 03:22:16
本文介绍了在Serializable Java类中使用Logger的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在制作的系统中有以下( doctored )类,并且 Findbugs 正在生成 SE_BAD_FIELD 警告,我正在尝试理解为什么会这样做在我按照我认为的方式修复它之前说。我感到困惑的原因是因为描述似乎表明我在类中没有使用其他非可序列化的实例字段,但bar.model.Foo也不是可序列化的并且以完全相同的方式使用(就我而言)可以告诉)但Findbugs没有生成任何警告。

I have the following (doctored) class in a system I'm working on and Findbugs is generating a SE_BAD_FIELD warning and I'm trying to understand why it would say that before I fix it in the way that I thought I would. The reason I'm confused is because the description would seem to indicate that I had used no other non-serializable instance fields in the class but bar.model.Foo is also not serializable and used in the exact same way (as far as I can tell) but Findbugs generates no warning for it.

import bar.model.Foo; import java.io.File; import java.io.Serializable; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Demo implements Serializable { private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final File file; private final List<Foo> originalFoos; private Integer count; private int primitive = 0; public Demo() { for (Foo foo : originalFoos) { this.logger.debug(...); } } ... }

我对解决方案的初步看法是在我使用它时从工厂获得记录器参考:

My initial blush at a solution is to get a logger reference from the factory right as I use it:

public DispositionFile() { Logger logger = LoggerFactory.getLogger(this.getClass()); for (Foo foo : originalFoos) { this.logger.debug(...); } }

但这似乎并不是特别有效。

That doesn't seem particularly efficient, though.

想法?

推荐答案

首先,不要过早优化。它可能是 LoggerFactory.getLogger()足够快,并且不会给执行时间带来很大的开销。如果有疑问,请对其进行分析。

Firstly, don't optimize prematurely. It may be that LoggerFactory.getLogger() is fast enough, and contributes no significant overhead to execution time. If in doubt, profile it.

其次,findbugs没有抱怨使用 Foo 是因为该类没有 Foo 类型的字段,它有一个 List 类型的字段。泛型在编译时被擦除,就字段定义而言,类中没有实际引用 Foo 。在运行时,如果您尝试序列化 Demo的实例类,但是findbugs不知道这个。

Secondly, the reason that findbugs isn't complaining about the use of Foo is because the class doesn't have a field of type Foo, it has a field of type List. The generics are erased at compile time, there is no actual reference to Foo in the class, as far as the field definition is concerned. At runtime, the fact that Foo is non-serializable would cause an exception if you tried to serialize an instance of the Demo class, but findbugs can't know this.

我的第一反应是让 Logger 静态字段,而不是实例字段。在这种情况下应该可以正常工作。

My first reaction would be to make the Logger a static field, rather than an instance field. Should work fine in this situation.

public class Demo implements Serializable { private static final Logger logger = LoggerFactory.getLogger(Demo.class); // .. other stuff }

更多推荐

在Serializable Java类中使用Logger的正确方法是什么?

本文发布于:2023-11-27 22:39:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639893.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类中   正确   方法   Serializable   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!