且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么在Java EE中使用CDI

更新时间:2023-12-03 13:33:34

写CDI的人给了你一个大对象工厂;他们为你做的工作比你更好。它是XML配置或注释驱动的,因此您不必将所有内容嵌入到代码中。

The people that wrote CDI gave you one big object factory; they did the work for you, better than you would. It's XML configuration or annotation driven, so you don't have to embed everything in code.

依赖注入引擎(如Spring)比您的工厂做得更多。它将需要多个工厂类和一行代码来复制它们提供的所有内容。

Dependency injection engines, like Spring, do a lot more than your factory. It'll take more than one factory class and one line of code to duplicate all that they offer.

当然,您不必使用它。你总是可以***发明自己的***。您应该 - 如果您的目的是学习如何制作***或消除依赖性。

Of course you don't have to use it. You are always free to invent your own wheel. And you should - if your purpose is to learn how to make wheels or eliminate dependencies.

但如果您只想开发应用程序,***使用其他人提供的工具,这些工具可以为您提供优势。

But if you want to just develop applications, it's better to use the tools that others provide when they give you an advantage.

关于依赖注入的开创性文章由Martin Fowler撰写。我建议阅读它;八年后,它仍然很棒。

The seminal article on dependency injection was written by Martin Fowler. I'd recommend reading it; it's still great, eight years later.


仍然不清楚更多的是什么

"still not clear on what the more is"

以下是一些优点:


  1. Looser coupling

  2. 更容易测试

  3. 更好的分层

  4. 基于界面的设计

  5. 动态代理(segue to AOP)。

  1. Looser coupling
  2. Easier testing
  3. Better layering
  4. Interface-based design
  5. Dynamic proxies (segue to AOP).