Google Guice Hello World

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:

Google Guice Hello World

  • 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:

  • HelloGuiceService.java:

  • HelloGuiceServiceImpl.java:

  • HelloGuiceModule.java:

  • HelloWorldGuiceMainClass.java:

  • Another way to inject object in the class example 2:

 

  • Now to run the main class right click –> Run As –> Java Application you will below Hello World message:

Google Guice Hello World

  • For more information please visit getting started guide here

download  Download project:  GoogleGuiceHelloWorld

Leave a Reply

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