Why use CDI in Java EE

前端 未结 4 1222
有刺的猬
有刺的猬 2020-12-12 13:55

I know there are a lot of articles out there that explain how to use CDI in Java EE but I\'m having trouble figuring out what advantage this actually brings. For example, su

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 14:36

    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.

    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.

    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"

    Here are a few advantages:

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

提交回复
热议问题