我正在考虑为slf4j logger使用CDI注入,所以我创建了一个 生产环境 者 .
我将它注入一个可序列化的 ApplicationScoped
bean:
@ApplicationScoped
public final class CurrentApplicationBean implements Serializable {
@Inject
private transient Logger logger;
}
它必须是瞬态的,因为 org.slf4j.Logger
是一个不扩展 Serializable
的接口,但这意味着必须在反序列化后重新注入 Logger .
I think that CDI doesn't handle that, what's you knowledge?
此外,提供程序始终提供一个新的 Logger
实例,因为它必须从 InjectionPoint
设置 Logger 名称,这意味着 RequestScoped
beans有自己的 Logger 实例而不是静态的每个类 Logger .
Maybe logging is not a good context for CDI injection... what are your considerations?
1 回答
CDI容器代理是可序列化的 . 反序列化时,代理定位/绑定到正确的注入 . 我不会将注射点标记为瞬态;因为这会阻止容器定位/重新注入注射并导致NPE .
如果您的 生产环境 者方法类似于以下内容
LoggerFactory.getLogger()为每个类创建一个 Logger .
这是你的选择 .