almost 10 years ago
Google Guice DI:- Google Guice is a Dependency Injection Framework that is same as Spring DI and use to achive IOC and High level of loose coupling to make the relationship between various Business Objects.
Add.java
Calculation.java
AddModule.java
- package add.service;
- import com.google.inject.Binder;
- import com.google.inject.Module;
- public class AddModule implements Module{
- public void configure(Binder binder) {
- binder.bind(Add.class).to(Calculation.class);
- }
- }
package add.service; import com.google.inject.Binder; import com.google.inject.Module; public class AddModule implements Module{ public void configure(Binder binder) { binder.bind(Add.class).to(Calculation.class); } }
AddClient.java
- package add.service;
- import com.google.inject.Guice;
- import com.google.inject.Injector;
- public class AddClient {
- public static void main(String[] args) {
- Injector injector = Guice.createInjector(new AddModule());
- Add add = injector.getInstance(Add.class);
- System.out.println(add.add(10, 54));
- }
- }
package add.service; import com.google.inject.Guice; import com.google.inject.Injector; public class AddClient { public static void main(String[] args) { Injector injector = Guice.createInjector(new AddModule()); Add add = injector.getInstance(Add.class); System.out.println(add.add(10, 54)); } }
Google Guice API:- following Interfaces/Classes are given below
Binder:- Binding refers a mapping of an Interface to its corresponding Implementation class. for example the interface Add is bound to Calculation implementation.
Injector:- Injectors is responsible for creating and maintaining Objects that are used by the Clients. That can take the Configuration information of creating and maintaining Relation-ship between Objects.
Module:- Modules are objects which will maintain the Bindings. It is possible to have multiple Modules in an Application. Injectors, in turn, will interact will the Modules to get the possible Bindings.
Guice:- Guice is a class which Clients directly depends upon to interact with other Objects. The Relation-ship between Injector and the various modules is established through this class.
0 Comment(s)