Java Dependency injection: XML or annotations

后端 未结 10 2239
既然无缘
既然无缘 2020-12-23 09:45

Annotations becoming popular. Spring-3 supports them. CDI depends on them heavily (I can not use CDI with out of annotations, right?)

My question is why

10条回答
  •  借酒劲吻你
    2020-12-23 10:07

    I just have a couple of things to add to what's already here.

    • To me, DI configuration is code. I would like to treat it as such, but the very nature of XML prevents this without extra tooling.

    • Spring JavaConfig is a major step forward in this regard, but it still has complications. Component scanning, auto-magic selection of interface implementations, and semantics around CGLIB interception of @Configuration annotated classes make it more complex than it needs to be. But it's still a step forward from XML.

    • The benefit of separating IoC metadata from application objects is overstated, especially with Spring. Perhaps if you confined yourself to the Spring IoC container only, this would be true. But Spring offers a wide application stack built on the IoC container (Security, Web MVC, etc). As soon as you leverage any of that, you're tied to the container anyway.

提交回复
热议问题