Google Guice Hello World
Google Guice (Known as Juice) is popular light weight dependency injection framework provided by Google. If you are working on project which need only dependency injection then this could be alternative of Spring IOC framework. To begining with it I will create very simple Hello world example where you will how dependency injection works:
- Create maven project GoogleGuiceHelloWorld below is final project structure:
- As you see in above project structure we have service and its implementation class. Model is responsible for binding service interface with its implementation class which provide functionality of dependency injection at run time. Finally HelloWorldGuiceMainClass is calling service to get data and using module to injecting members. To use you will have include guice-3.0.jar (As of writing this version 4.0 beta was released). We will use version 3.0 here. Please include its dependencies in pom.xml as below:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javahonk</groupId> <artifactId>GoogleGuiceHelloWorld</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>GoogleGuiceHelloWorld Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>3.0</version> </dependency> </dependencies> <build> <finalName>GoogleGuiceHelloWorld</finalName> </build> </project>
- HelloGuiceService.java:
package com.javahonk.service; /** * Created by Java Honk on 4/9/2015. */ public interface HelloGuiceService { public String serviceMethod(String msg); }
- HelloGuiceServiceImpl.java:
package com.javahonk.service; /** * Created by Java Honk on 4/9/2015. */ public class HelloGuiceServiceImpl implements HelloGuiceService { @Override public String serviceMethod(String msg) { return "Google Guice "+ msg; } }
- HelloGuiceModule.java:
/* * 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.javahonk.service.HelloGuiceService; import com.javahonk.service.HelloGuiceServiceImpl; public class HelloGuiceModule extends AbstractModule { protected void configure() { // add configuration logic here bind(HelloGuiceService.class).to(HelloGuiceServiceImpl.class); } }
- HelloWorldGuiceMainClass.java:
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/9/2015. */ public class HelloWorldGuiceMainClass { @Inject HelloGuiceService helloGuiceService; public static void main(String[] args) { HelloWorldGuiceMainClass helloWorldGuiceMainClass = new HelloWorldGuiceMainClass(); Module module = new HelloGuiceModule(); Injector injector = Guice.createInjector(module); injector.injectMembers(helloWorldGuiceMainClass); helloWorldGuiceMainClass.helloWorldGuice(); } private void helloWorldGuice() { String testStr = helloGuiceService.serviceMethod("Hello World!"); System.out.println(testStr); } }
- Another way to inject object in the class example 2:
package com.javahonk; import com.google.inject.Guice; 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/9/2015. */ public class HelloWorldGuiceMainClass2 { public static void main(String[] args) { HelloWorldGuiceMainClass2 helloWorldGuiceMainClass = new HelloWorldGuiceMainClass2(); Module module = new HelloGuiceModule(); Injector injector = Guice.createInjector(module); HelloGuiceService helloGuiceService = injector.getInstance(HelloGuiceService.class); helloWorldGuiceMainClass.helloWorldGuice(helloGuiceService); } private void helloWorldGuice(HelloGuiceService helloGuiceService) { String testStr = helloGuiceService.serviceMethod("Hello World!"); System.out.println(testStr); } }
- Now to run the main class right click –> Run As –> Java Application you will below Hello World message:
- For more information please visit getting started guide here
Download project: GoogleGuiceHelloWorld