Skip to main content

More Guice Please!!!: Re-Learning Google's Agile Lightweight Dependency Injection Library (Part 1.4)

Let’s see if we can go through some simple examples starting with a typical XService/XServiceImpl example.  In this case, after creating the appropriate XService trait and XServiceImpl extending it, you could extend AppNameModule in the following way:

Listing 1.4: Example of Module binding XService and XServiceImpl
package com.uequations.demo.module

import com.uequations.demo.service.{XService, XServiceImpl}

object XServiceModule extends AppNameModule {

override def configure(): Unit = {
  bind(classOf[XService]).to(classOf[XServiceImpl])
}
}
Below demonstrates code using the XService/XServiceImpl binding from (1.4):

Listing 1.5: Example of how to make use of binding
package com.uequations.demo.app

import com.google.inject.Guice
import com.uequations.demo.module.XServiceModule
import com.uequations.demo.service.XService

object DemoApp {

def main(args:Array[String]): Unit ={
  val injector = Guice.createInjector(XServiceModule)
  val instance = injector.getInstance(classOf[XService])
  instance.doSomething()
}
}

Here, we use the static createInjector method, but there are other ways of making use of a binding.

Comments

Popular posts from this blog

#processing @Microsoft #office #Excel files with @TheASF POI (part II)

...
     Apache POI's OPCPackage abstract class represents a container that can store multiple data objects.  It is central to the processing of Excel(*.xlsx) files.  We only need to use its static open method to process an InputStream instance.  Further, we can "read" these Excel files via the XSSFWorkbook class.  This class is a high level representation of a SpreadsheetML workbook.  From an XSSFWorkbook, we can get any existing XSSFSheets within the workbook.  Then, we can further subdivide any XSSFSheet into rows and analyze the cell data within the rows.  In general, given certain assumptions in the format of the Excel document, we can extract data as text  from a cell and perform any number of business processes.

     In the Java function code excerpt below, we assume we have an Excel(*.xlsx) file represented as an InputStream.

        @Override
    public Iterator<Row> apply(InputStream inputStream) {

        try(OPCPackage pkg = OPCPackage.open(…

More Guice Please!!!: Re-Learning Google's Agile Lightweight Dependency Injection Library (Part 1.1)

Google Guice is used as a lightweight dependency injection framework that further assists developers in modularizing their applications.  Google shared this very useful library with the development community in 2010 in between the Java SE 6 and Java SE 7 releases.  This library is used in some of Java’s (and now Scala’s) most prominent libraries and platforms such as the Simian Army platform shared by Netflix.
We will begin our discussion of Google Guice with its Module interface.  In the Guice developers’ own words, ‘A Guice-based application is ultimately composed of little more than a set of modules and some bootstrapping code.’  We will not be using this interface directly, but it gives us a very good context from which to start.  Instead, we will be extending the abstract class that implements it -- intuitively named AbstractModule.  
If you ever get a chance to look at the Module interface JavaDoc or source code, you’ll see a configure method taking a parameter of type Binder.  
Li…

A Tale of Two Log Forwarders: Logstash and FluentD (part 1.2)

A couple of ways to install Logstash is as a service on the cloud virtual machine, as well as deployed as a Docker container.  For our example, let's deploy Logstash as a Docker container. Assuming the Docker engine in installed, we can pull it from the docker.elastic.co repository: docker pull docker.elastic.co/logstash/logstash   You can execute the "docker image" command to verify the image was pulled.  Before we create a container from this image, we'll create a configuration file.   It is possible to have multiple inputs on a single Logstash configuration file, but let us, for this example, have one configuration file  per input. vim tcp-to-elasticsearch.conf input {   tcp { port => 8080   } } output {   elasticsearch { hosts => ["10.0.0.41:9200"]   }   stdout {