As we have seen in previous post about how to Set up Dependency Injection using Google Guice API with Example. It is very easy to set up Dependency Injection using Google Guice as we don’t need any configuration xml unlike popular Spring Framework. As per the Concept we bind the Interface with Implementation in AbstractModule, but what if one interface has multiple implementation and we want Google Guice to resolve the Multiple Dependency and Inject the required Object at runtime.
In this post we will see how to overcome this problem. Let’s see the component one by one.
Configure Module to Enable the Dependency Injection :
To bind the interface with Implementation we need to create the Module. Guice provides two ways to resolve the Multiple Dependency -
1) Using Annotation.
2) Using Named Annotation (String)
1) Using Annotation –
1. As shown below we need to create the SampleModule.Java and specify the binding for Interface to implementation through annotation.
As you can see we have created Two Annotation JMS.java and REST.Java and bind the Interface with Implementation via Annotation for example Client Interface will be bind to RESTClient and RESTServiceImpl when marked with REST annotation.
Following is the definition for the Both JMS and REST Annotation –
2. Interfaces and Implementation – As shown in the above point we need to Create the Interface and Corresponding Implementation. For Example, JSMClient and RESTClient are the type of Client which can access the Service ( JMSService / RESTService).
As you can notice Guice will Inject restService as per binding defined using REST Annotation for interface WebService.
As you can notice Guice will Inject restService as per binding defined using JMS Annotation for interface WebService.
3. Services and Implementation – As shown in SampleModule following are the Service and Corresponding Implementation –
4. Main Application / Execute Program –
As shown in the below code you can see how Guice is resolving the dependency by injecting the instance of jmsClient and restClient based on the Annotation specified @JMS and @REST
2) Using Named Annotation –
Creating new Annotation types for every concrete implementation might not be useful as the sole purpose of having such an Annotation is just to mark the Implementation class instance needed by the Clients. Instead of Creating our own Annotation, we can use @Named annotation which can be use to provide the name to the binding.
There is one utility method Names.named() which returns @Named annotation. This can be use to mark the Implementation with @Named annotation.
1. Sample Module for the Named Annotation –
As you can see we have used Names.named() to mark the Implementation. This approach is easy to use and handy.
2. Interfaces and Implementation –
Access the Define Mapping in SampleModule using @Named Annotation as shown below.
3) Services and Implementation will be same as there is no dependency Involved.
4. Inject dependency in MainApplication using Named Annotation
As shown below injected the jmsClient and restClient. Rest of the code will be same.
Entry Point of Application –
In the Above mentioned way we can get the instance of Injector from the sameple Module. We can get the Instance of any class defined in the project using the Injector with condition either have a Default Constructor or Parameterized constructor with @Inject annotation.
How to Write JUnit test cases for Google Guice –
To Write the test case for functionalities using Guice, we need to Create the instance of Injector using the Module. We can Install the SampleModule which we had created.
Following is the example to show case –
This post is written by
Soumitra Pathak - Linkedin, Facebook
He is a freelance writer, loves to explore latest features in Java technology.