73

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 with Java EE 6 standard, therefore we are analyzing if CDI can be used instead of Spring.

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

Kayvan Tehrani
  • 3,070
  • 2
  • 32
  • 46
prassee
  • 3,651
  • 6
  • 30
  • 49

3 Answers3

109

Update 2021: It's been 10 years since I answered the original question, and I'm still getting the occasional upvote. Can I just ask future readers to take my response as what it is: A reflection of the Java Enterprise landscape in 2011.

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.

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).

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.

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.

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 ;-)

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

Jan Groth
  • 14,039
  • 5
  • 40
  • 55
  • I think CDI use static inyection like Spring. According to [Weld documentation](http://docs.jboss.org/weld/reference/1.1.0.Final/en-US/html_single/#injection) `The @Inject annotation lets us define an injection point that is injected during bean instantiation.` As I understand it, dynamic injection is injection that occurs more than once. It occurs every time a business method is called on that component. Also for me, `stateful way inyection` means the ability to handle injection of beans from different contexts, it uses proxies that reference the “correct” instance of each bean – Victor Martinez Sep 10 '11 at 13:58
  • 11
    No, that is not correct - I don't follow your definition of dynamic injection. The dynamic part of CDI is rather that dependencies are proxied (as your write) and that the proxy will take care to resolve the right dependency whenever requested (so two invocation of a dependency may be forwarded to two different instances of that dependency). But maybe a comment is not the right place to start a discussion, you might want to open a new question for that... – Jan Groth Sep 10 '11 at 14:52
  • 1
    Is this still the case now? –  May 31 '17 at 00:38
  • 5
    `Spring's approach is static`. I don't think this is completely accurate (at least currently). Spring also provides other scopes (such as the `prototype` scope), which resolves beans and wires at runtime during the execution of the request. – Dimos Dec 10 '17 at 19:31
13

Spring is more than just a dependency injection container. It also has tools for AOP, templates for use with JPA, SQL, etc. and even more.

However CDI can be used as a replacement for Spring's DI API.

Behrang
  • 46,888
  • 25
  • 118
  • 160
  • 7
    I think CDI handles the AOP via Interceptors – prassee May 12 '11 at 05:04
  • 1
    Interceptors are very similar to AOP in general, but they do not have the breadth of features that an AOP framework or language like AspectJ provides. – Behrang May 12 '11 at 05:06
  • 4
    You can write extensions which add interceptors based on your rules to beans. That's very easy. For most applications extensive usages of AOP are too complex to be useful. – Dar Whi Sep 07 '11 at 22:18
  • Everything that can be achieved via AOP can be achieved with CDI Interceptors. Please consider not writing religious answers. –  Jun 13 '17 at 15:48
4

I'm using Apache OpenWebBeans as CDI implementation and MyFaces CODI as portable extension for several projects. I'm very happy with it and I had no problems with it. OpenWebBeans currently lacks a bit in view of documentation but if you can't get something to work it's pretty easy to use the Maven Archetypes provided by MyFaces to generate simple projects with all the needed dependencies or you ask on the mailing list. It's so great if you just work on your Application and you aren't blocked by nasty bugs. I also did a lot of projects with Spring. It's ok, but if you ask what I would use for the next project the clear answer is OpenWebBeans and CODI! I prefer OpenWebBeans over Weld because OpenWebBeans is very adoptable that's great because you can customize more or less everything which isn't covered by the official CDI API/SPI and the runtimeperformance is better. And after the first project I would never question again CODI because it's very stable, they have regular releases and most of them brought great new features which improve the productivity a lot. CODI is IMHO the place which is most stable and were most innovations come from in the whole CDI land.

To answer your question: For me CDI completely replaced Spring, but you need portable extensions which fill the gaps. CDI as standard never intended to solve everything and some parts like the conversations are broken by design. The good news is that you have great projects like MyFaces CODI. CODI fixes almost all those issues.

Dar Whi
  • 822
  • 5
  • 14