简体   繁体   English

通过 CDI 注入记录器,一种反模式?

[英]Inject logger by CDI, an anti-pattern?

Looking at this code:查看这段代码:

@Stateless
public class AStatelessEJB {

@Inject
private Logger logger;

(...)

Why a logger, which is typically final and static, is injected by CDI into a stateless EJB (as an instance variable)?为什么 CDI 将一个通常为 final 和 static 的记录器注入无状态 EJB(作为实例变量)?

Is there any advantage?有什么好处吗? Or only disadvantages such as:或者只有缺点,例如:

  • overhead of CDI lookup (per each bean initialization) CDI 查找的开销(每个 bean 初始化)
  • overhead of CDI initialization (per each bean initialization) CDI 初始化的开销(每个 bean 初始化)
  • overhead of CDI injection (per each bean initialization) CDI 注入的开销(每个 bean 初始化)
  • overhead of garbage collector clean up (each time the bean gets destroyed)垃圾收集器清理的开销(每次 bean 被销毁时)

if you evaluates it from a performance perspective view... there is no big differences but static initialization usually is better than a managed bean. 如果从性能透视图评估它...没有很大的区别,但静态初始化通常比托管bean更好。

Usually, CDI will require a little more processing at application initialization and at contextual instance creation time. 通常,CDI在应用程序初始化和上下文实例创建时需要更多处理。 It will require a few more lines inside a producer method/class but will provide easy of use and certain kind of flexibility. 它将在生产者方法/类中需要更多行,但是将提供易用性和某种灵活性。 Btw with CDI you can manage one instance per class or application with a minimal producer method. 顺便说一句,使用CDI,您可以使用最小的生产者方法管理每个类或应用程序的一个实例。

Advantages: 好处:

  • Easy to provide abstraction from common logging frameworks 易于提供常见日志框架的抽象
  • Easy to add support for additional features like internationalization and localization 易于添加对国际化和本地化等附加功能的支持
  • Easy of use (simple like use an annotation) 易于使用(简单如使用注释)
  • Easy of manage multiple implementations/configurations (simple like use multiple qualifiers) 易于管理多个实现/配置(简单如使用多个限定符)
  • Standardize use (like other fields) 标准化使用(像其他领域一样)

Disadvantages: 缺点:

  • Additional minimal processing cost 额外的最低处理成本
  • Other tools like Lombok provides easy of use and provides annotation configuration too (at compile time and in a static way). 像Lombok这样的其他工具也提供了易用性并提供了注释配置(在编译时和静态方式)。
  • Usually is not required additional features for logs (like internationalization/localization). 通常不需要日志的其他功能(如国际化/本地化)。

In my opinion and summarized, I prefers to use an static field but it is not a bad practice use a cdi bean if you want certain point of abstraction, provide additional features, provide multiple Logger implementations/configurations or provide something ease of use to developers. 在我看来并总结一下,我更喜欢使用静态字段,但如果你想要某些抽象点,提供额外的功能,提供多个Logger实现/配置或者为开发人员提供易用性,那么使用cdi bean并不是一个坏习惯。 。

I think Ariel's answer is correct.我认为 Ariel 的回答是正确的。 I wanted to add that you may have a fundamental misunderstanding of how CDI works: CDI does not inject the beans directly, nor does it instantiate the beans for every injection point.我想补充一点,您可能对 CDI 的工作原理有一个根本性的误解:CDI 不会直接注入 bean,也不会为每个注入点实例化 bean。 CDI Injects proxies, which have a reference to the real bean. CDI 注入代理,它具有对真实 bean 的引用。 These proxies are dirt cheap to create and gc.这些代理的创建和 gc 非常便宜。 SL4J is also pooled and optimized for quick logger lookup. SL4J 也被合并和优化以用于快速记录器查找。 Combine these two, there are likely far more previous items that would affect your application that a very lightweight CDI injection.将这两者结合起来,可能会有更多的先前项目会影响您的应用程序,而不是非常轻量级的 CDI 注入。

When doing any performance testing, the number one rule is to use a profiler.在进行任何性能测试时,首要规则是使用分析器。 I think getting yourself a good test setup where you can evaluate your assumptions would be key.我认为让自己拥有一个可以评估假设的良好测试设置将是关键。 That being said, I think CDI Injections will likely be as-fast or faster.话虽这么说,我认为 CDI 注入可能会一样快或更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM