CDI是否可以很好地替代Spring? [英] Is CDI a good replacement of Spring?

查看:254
本文介绍了CDI是否可以很好地替代Spring?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正计划从头开始编写一个Web应用程序,已决定使用符合Java EE 6标准的最新版本的Glassfish,因此我们正在分析是否可以使用CDI代替Spring.

We are planning to write a web application from the scratch, it has been decided to go with the latest edition of Glassfish which complies to Java EE 6 standard, therefore we are analyzing if CDI can be use instead of Spring.

我们可以说CDI可以替代Spring吗?

Can we say that CDI could be a replacement for Spring?

推荐答案

CDI代表上下文和依赖注入",而Spring是围绕依赖注入容器的完整生态系统.要比较两者,您必须区分比较.

CDI stands for "context and dependency injection", while Spring is a complete ecosystem around a dependency injection container. To compare both, you have to differentiate the comparison.

依赖项注入由两个容器处理.主要区别在于CDI以 dynamic (又称有状态")方式处理DI的事实-这意味着依赖项在执行时间得以解决. Spring的方法是 static -这意味着组件在创建时间连接在一起.乍一看,CDI方式似乎有点不寻常,但它优越得多,并提供了更多高级选项(我是在两个高效CDI应用程序的背景下编写的).

Dependency injection is handled by both containers. The main difference is the fact that CDI handles DI in a dynamic (aka: stateful) way - this means that dependencies are resolved at execution time. Spring's approach is static - this means that components are wired together at creation time. While the CDI-way might seem a bit unusual at a first glimpse, it's far superior and offers way more and advanced options (I'm writing this with the background of two productive CDI apps).

如果您查看生态系统,情况就不同了:Spring捆绑了许多 罐(> 150),而CDI本身就很小.典型的CDI使用将在Java EE 6应用服务器内部,但是您可以轻松地使其在servlet引擎甚至Java SE中运行.这意味着使用CDI并不需要假设您要使用Hibernate,JPA,EJB或其他任何方法-这取决于您.

If you look at the ecosystem, the situation is different: Spring comes bundled with a lot of jars (>150), while CDI is pretty small by itself. A typical CDI-usage would be inside of a Java EE 6 application server, but you can easily make it work in a servlet engine or even Java SE. This means that using CDI makes no assumption about using Hibernate, JPA, EJB or whatever - that's up to you.

如果您需要更多功能,则CDI带有便携式扩展(本身使API值得使用)的概念.存在诸如Apache CODI和Seam 3之类的独立扩展模块,并涵盖诸如安全性,邮件,报告等主题.

If you need more functionality, CDI comes with the concept of portable extensions (which by itself makes the API worthwile). Independent extension modules like Apache CODI and Seam 3 exist and cover topics like security, mailing, reporting and more.

总结:CDI并不像Spring生态系统的替代品",而是对Spring依赖注入机制的改进.它是Java EE 6的一部分,因此,如果您使用的是带有Java EE 6的GlasFish,则绝对应该使用CDI.在我看来,您的问题是:我可以用Java EE 6代替Spring吗?我想我的答案很明显;-)

To summarize: CDI is nothing like a "replacement" for the Spring ecosystem, it's rather an improvement over Spring's dependency injection mechanism. It's part of Java EE 6, so if you are on a GlasFish with Java EE 6, you should definitely go for CDI. In my eyes your question is rather: Can I replace Spring with Java EE 6? I guess my answer is pretty obvious ;-)

看看 Weld ,这是一个很好的开始...

Have a look at Weld to get a good start...

这篇关于CDI是否可以很好地替代Spring?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆