In association with heise online

No need to fear injections

Guice manages dependencies in the form of bindings that consist of the object type, annotation and scope that are to be generated. The provider supplies the object to the injector (Fig. 2).
Guice manages dependencies in the form of bindings that consist of the object type, annotation and scope that are to be generated. The provider supplies the object to the injector (Fig. 2).
The injector, which manages the bindings, is a key element in the structure of Guice (Figure 2). The bindings use an annotation at the point of injection, refering to the type that is to be injected, and the provider generates the instances of that type. The scope of a binding is an optional item of information that controls the reuse of generated and injected objects. By default, Guice creates a new instance of the object concerned for each injection. Singleton, request and session are alternative scopes.

Guice can handle more extensive possibilities that go well beyond the fundamental mechanisms described here. It can define custom annotations, say to permit the further assignment of flight and car hire bookings.

Normally, when you hear the words "dependency injection" or "inversion of control" you first think of Spring. There's a good reason for that, of course: it's the de facto standard and has been assembled by an active community. As already mentioned, however, despite its existence Google has seen a need for its own solution. Not even alternatives like JBoss Seam, Apache HiveMind or Pico-Container could dissuade Google from producing something in-house.

Both frameworks – Spring and Guice – are open-source products, covered by the Apache 2.0 licence. As annotations and generics are components of Guice, Java 5 or later is required. That requirement doesn't apply to Spring, which can even be used with JDK 1.3, besides providing many more functions than Guice. While Guice concentrates on DI, Spring also supports transactions and persistence, and possesses its own web framework. Various sub-projects exist, such as for configuration, security and batch jobs.

The definition of dependencies between objects forms the backbone of a DI framework, and the way dependencies are stored and evaluated (the wiring of the objects) is a characteristic property. This brings up a further substantial difference between Spring and Guice. Spring permits both explicit representation and auto-wiring. Guice follows a different approach: although it's based on an explicit representation, it sidesteps the verbose XML format by housing the associations in the source code with Java annotations. This can can be seen as a mixture of explicit representation (thorough, but high-maintenance) and auto-wiring. Spring also permits the creation of dependencies in Java code using JavaConfig, but this sub-project is still at milestone level.

Next: Small, fast, and crystal clear

Print Version | Permalink: http://h-online.com/-746531
  • Twitter
  • Facebook
  • submit to slashdot
  • StumbleUpon
  • submit to reddit
 


  • July's Community Calendar





The H Open

The H Security

The H Developer

The H Internet Toolkit