Google Guice

from Wikipedia, the free encyclopedia
Google Guice

Guice.jpg
Basic data

developer Google Inc. ( more )
Current  version 4.2.3
( March 19, 2020 )
operating system platform independent
programming language Java
category Dependency injection framework
License Apache license
German speaking No
github.com/google/guice

Google Guice is a open source - Framework for Dependency Injection with Java . Guice was first released by Google in 2008 under the Apache license . As the first framework at the time, Guice enabled dependency injection using annotations . In 2008 it won the Jolt Award for the best framework.

Dependency Injection (DI) and Inversion of Control (IoC)

Inversion of Control is a pattern in software development in which programs do not generate their dependencies (ie program parts or components on which they themselves depend) themselves, but can be "given in" from outside. This principle is basically possible without dependency injection , but is considerably simplified with dependency injection. Dependency Injection provides that the components are not generated by the user himself, but are generated by an "injector" (that's the name in Google Guice). This injector can generate concrete objects for all necessary “sub-components” and thus also provide the requested, often complex object. For this purpose, the specific classes to be used for the injector (e.g. for interfaces) are specified. In Google Guice, this is done using so-called modules.

Modules in Google Guice

Modules in Guice summarize the dependencies to be used for a certain "subject area". How far this "topic" is understood is left to the individual programmer and can vary greatly from software project to software project. What the modules have in common is that they map or “bind” specific classes to be used to (often abstract) dependencies in the project. So z. For example, it can be specified that an object of the type “NetworkMailSender” is injected at every point where a type from the “MailSender” interface is used. Due to the central mapping of the types to be used in the modules of the injector, the objects actually used can be exchanged very easily. So z. For example, mock objects (e.g. MailSenderMock) for the “MailSender” interface can be linked very easily in unit tests, which simulate the function of sending mail and thus make simple, fast, independent and stable test cases possible.

Scopes

For the objects to be used, you can define how long the objects that have been created should be “cached” (ie reused). This information is called "Scope" in Guice. The most popular scopes are the "default scope" (no explicit scope specification), in which a new object is created for each request of the type, or the "singleton scope" in which program-wide - controlled by the injector - only an object of this type exists. The scopes can be expanded for your own purposes.

See also

Web links

Individual evidence

  1. Release 4.2.3 . March 19, 2020 (accessed March 20, 2020).
  2. 18th Annual Jolt Award winners