Google Guice vs JSR-299 CDI / Weld

Weld, an implementation of JSR-299 injection contexts and dependencies, sees itself as a kind of successor to Spring and Guice.

CDI has been influenced by many existing Java frameworks, including Seam, Guice, and Spring. However, CDI has its own, very distinct character: more typical than Seam, more restrained and less XML-oriented than Spring, more web and enterprise applications than Guice. But it could not be any of them without inspiration from the specified framework and a lot of cooperation and hard work of the JSR-299 Expert (EG) group.

http://docs.jboss.org/weld/reference/latest/en-US/html/1.html

What makes Weld more capable for enterprise applications than Guice? Are there any advantages or disadvantages compared to Guice? What do you think of Guice AOP compared to Weld interceptors? What about performance?

My choice

In the end, I decided to use Guice because I like the clean programming model, which by default has almost no annotations except @Inject. It is much easier to use external libraries with Guice than with CDI. AOP is also quite simple with Guice.

+57
java dependency-injection aop cdi jboss-weld
Apr 16 '10 at 10:22
source share
5 answers

Before trying to answer your question, let me add some important information: JSR 330 ( @Inject ) was standardized by the Guice and Spring projects ( reused in JSR 299. This covers the basic DI mechanisms in terms of declaring an injection point.

Now back to the question - with the waiver that I have much more experience with Spring than with Guice.

Enterprise Opportunities at Weld

  • Alternative tuning mechanisms have a very clean design in JSR-299 and allow you to use tuning mechanisms outside of Java code ( beans.xml ).
  • Events is a very powerful thing and goes well with JMS. I just found Event Bus for Guice , but I can’t say how it compares.
  • Portable extensions are SPIs that can be used to integrate with existing technology or to port legacy code in its purest form.

Advantages disadvantages

Note. I will try to add a few elements here later, but this answer is already longer than I expected, sorry.

  • welding / CDI

    • Standardization: if something is standardized and there is a good implementation, many people will use it. Example: The built-in areas in Weld provide several additional areas than Guice or Spring . All of them can be expanded, but application frameworks will rather rely on standard areas if they are used by a large community.
    • Container support: this is similar to the previous item, but the main argument for adoption. Major open source application servers, such as Glassfish and JBoss 6, support CDI (see here ).
  • Guice / spring

    • Actual application: most existing applications will already use Guice / Spring. Spring / Guice always built standards and provided new opportunities when standards did not exist or could not be used. If you follow relevant best practices, the framework will help you make your applications standard and clean.

AOP and interceptors

This is a very discussed topic, and I cannot approve of it. Both mechanisms are very powerful, but require a minimal understanding of application architecture. Also see Decorators and the previously mentioned Events . It is best to go with the right tool, but do not forget that if a developer has to work with one of these mechanisms, it is good if he / she understands the concept.

Performance

Unfortunately, I could not learn this yet, but there are a few rules that I'm trying to follow, especially when using a framework that gives you a lot of functionality if you don't notice it:

  • If possible, prefer one step to connecting to multiple requests at run time.
  • If possible, complete all wiring when initializing the application.
  • Any capture step or AOP proxy adds multiple label calls to the stack.
+51
May 11 '10 at 1:45
source share
— -

CDI (Weld) is not yet widely used, so comparison is difficult to do. A few points:

  • CDI has been designed with EJB3, JSF, and other JavaEE standards in mind. CDI has so-called portable extensions that allow third-party libraries to integrate with the life cycle and internal functioning of the CDI implementation.
  • CDI was designed with all angular cases in mind, so it is likely that it covers everything you need. Spring, Guice, and Seam have evolved into this state, while CDI uses the experience of the three.
  • in my opinion, CDI interceptors will not be able to satisfy all the requirements that Spring AOP met. Perhaps the same goes for Guice AOP. You cannot define an interceptor using AspectJ syntax.
  • the absence of xml definitions is both an advantage and a disadvantage, and some people (in truth, in some cases) prefer the xml configuration.
  • expanded use of qualifier annotations will (in my opinion) generate some big mess if not used carefully.
+18
Apr 16 2018-10-11T00:
source share

The most important feature of CDI, contrary to Guice, is that it is a standard part of Java EE 6.

The value of this should not be underestimated, since it means that CDI is the DI standard that you should use when coding web applications.

While I looked back at technology to be able to determine how we could have a standard core distribution — suitably prepared — where we could add additional modules as we saw fit, which could override existing functionality without having to change core modules, that is, add an additional jar, and the function is automatically activated.

It turned out that the best way to do this for the code base used in both desktop and web applications is to use JSR-330 annotations for our code, and then use either CDI or Guice (SVN, coming soon in 3.0) as engine.

After several projects, I found it best to use the Guice configuration instead of the opaque magic happening in Weld. In addition, I found that the way to do what we want, as described above using Weld, I should mark the class in the optional jar as @Alternative, and then mention in beans.xml that I want the alternate class to be forced ( and that is not reliable against refactoring).

But overall, the JSR-330 allows us to do something that was very tiring and fragile before (because new so tightly bound), and this is a great win. I can highly recommend finding a DI if you have such a need.

+8
Dec 20 '10 at 19:02
source share

CDI for Guice users is a good comparison.

+8
Oct. 29 '13 at 13:18
source share

Another difference is that CDI is very Java EE oriented. It provides a mechanism to glue various Java EE subsystems .

Those. @Named("book") bean with @Named("book") , the bean becomes known in the unified EL (expression language) as a " book ".

Then you can use it on the JSF page, for example:

  <h:outputLabel value="Book title:" for="bookTitle"/> <h:outputText id="bookTile" value="#{book.title}"/> 
+5
05 Nov '10 at 12:20
source share



All Articles