Guice provider bindings example

Guice provider bindings example

If you are using Google Guice framework for dependency injection in your application you could use provider interface, which has method to supplying its values. In this demo you will see how to use Guice provider interface to get an object of implementing class at run time, call its method which return string to and print its value on the console:

  • Provider interface:
public interface Provider<T> {
  T get();
}
  • HelloGuiceService interface:
package com.javahonk.service;

/**
 * Created by Java Honk on 4/19/2015.
 */
public interface HelloGuiceService {
    public String serviceMethod(String msg);
}
  • HelloGuiceServiceImpl.java:
package com.javahonk.service;

/**
 * Created by Java Honk on 4/19/2015.
 */
public class HelloGuiceServiceImpl implements HelloGuiceService {

	@Override
	public String serviceMethod(String msg) {
		return "Google Guice "+ msg;
	}
   
}
  • HelloGuiceServiceProvider.java:
package com.javahonk.service;

import com.google.inject.Provider;

/**
 * Created by Java Honk on 4/19/2015.
 */
public class HelloGuiceServiceProvider implements Provider<HelloGuiceService>{
	
	@Override
	public HelloGuiceService get() {
		HelloGuiceService helloGuiceService = new HelloGuiceServiceImpl();
        return helloGuiceService;
	}
	
}
  • HelloGuiceModule.java: It’s module class where you define binding of the class. Below it says interface HelloGuiceService provider class is HelloGuiceServiceProvider which return object of HelloGuiceServiceImpl class. In Singleton.class means it’s behavior is singleton where it will return only one instance of the class:
/*
 * Created by IntelliJ IDEA.
 * User: Java Honk
 * Date: 4/9/2015
 * Time: 9:26 PM
 */
package com.javahonk.module;

import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import com.javahonk.service.HelloGuiceService;
import com.javahonk.service.HelloGuiceServiceProvider;

/**
 * Created by Java Honk on 4/19/2015.
 */
public class HelloGuiceModule extends AbstractModule {

	protected void configure() {
		bind(HelloGuiceService.class).toProvider(HelloGuiceServiceProvider.class).in(Singleton.class);

	}
}
  • ProviderServiceMainClass.java is java main class to test our implementation and print message on the console:
package com.javahonk;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.javahonk.module.HelloGuiceModule;
import com.javahonk.service.HelloGuiceService;

/**
 * Created by Java Honk on 4/19/2015.
 */
public class ProviderServiceMainClass {

    @Inject
    HelloGuiceService helloGuiceService;

    public static void main(String[] args) {
        
    	ProviderServiceMainClass providerServiceMainClass = new ProviderServiceMainClass();

        Module module = new HelloGuiceModule();
        Injector injector = Guice.createInjector(module);
        injector.injectMembers(providerServiceMainClass);

        providerServiceMainClass.providerTest();
    }

    private void providerTest() {
        String testStr = helloGuiceService.serviceMethod("Hello World!");
        System.out.println(testStr);
    }
}

Guice provider bindings example

  • For more information please visit Guice documentation here

download  Download project:  GoogleGuiceProvider

Leave a Reply

Your email address will not be published. Required fields are marked *