Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Google Guice Dependency Injection(DI)

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 398
    Comment on it

    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

    package add.service;
    public interface Add {
        public int add(int a, int b);
    }
    

    Calculation.java

    package add.service;
    public class Calculation implements Add{
            public int add(int a, int b) {
                return a + b;
            }
    }
    

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

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

    Google Guice API:- following Interfaces/Classes are given below

    1. Binder
    2. Injector
    3. Module
    4. Guice

    Binder:- Binding refers a mapping of an Interface to its corresponding Implementation class. for example the interface Add is bound to Calculation implementation.

                   binder.bind(Add.class).to(Calculation.class)
    

    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.

                     Add addObject = injector.getInstance(Add.class)
    

    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.

    class CustomModule extends AbstractModule
    { 
             public void configure(Binder binder)
             {
             }
     }
    

    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.

    CustomModule module = new CustomModule();
    Injector injector = Guice.createInjector(module);
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: