guice

Getting started with guice

Remarks#

This section provides an overview of what guice is, and why a developer might want to use it.

It should also mention any large subjects within guice, and link out to the related topics. Since the Documentation for guice is new, you may need to create initial versions of those related topics.

Setup of a ‘Hello, world!’ example

Guice is a Java library. To use it you have to add a JAR file into the classpath of your Java project.

Sample classes

Below are several classes for a “Hello, world!” example.

An interface of a hello “service”:

public interface HelloWorldService {
    public void sayHello();
}

The implementation of the service:

public class HelloWorldServiceImpl implements HelloWorldService {
    @Override
    public void sayHello() {
        System.out.println("Hello, world!");
    }
}

A Guice naming module. It is needed to instruct Guice that HelloWorldServiceImpl will be injected where a hello service is needed.

import com.google.inject.AbstractModule;

public class HelloWorldModule extends AbstractModule {
    protected void configure() {
        bind(HelloWorldService.class).to(HelloWorldServiceImpl.class);
    }
}

A main class where the actual injection of a hello service takes place:

import javax.inject.Inject;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;

public class Main {

    @Inject
    private HelloWorldService service;//hello service
    
    public static void main(String[] args) {
        
        Main main = new Main();
        
        Module module = new HelloWorldModule();
        Injector injector = Guice.createInjector(module);
        injector.injectMembers(main);//injects the implementation of the service
        
        main.testGuice();
    }

    public void testGuice()
    {
        service.sayHello();//usage of the service
    }
}

Run with Gradle

To quicky setup and run with Gradle 2.2.+ and Java 8:

  1. Install gradle if not already installed

  2. Create an empty directory and navigate into it with a gradle enabled shell

  3. Create an empty java project:

    gradle init --type java-library

  4. In the automatically generated build.gradle:

  • change apply plugin: 'java' to apply plugin: 'application'

  • add the following line

    mainClassName = 'Main'

  • in the dependencies section add a dependency to a version of guice, e.g.:

    dependencies {
      ...
      compile group: 'com.google.inject', name: 'guice', version: '4.1.0'
      ...
    }
  1. Add the classes shown above into the default package in src/main/java, each in its own file

  2. Run and enjoy

    ..> gradlew run
        :compileJava
        :processResources UP-TO-DATE
        :classes
        :run
        Hello, world!
    
        BUILD SUCCESSFUL
    
        Total time: 3.595 secs

Run with Maven

To quicky setup and run with Maven 3+ and Java 8:

  1. Install maven if not already installed

  2. Create an empty directory and navigate into it with a maven enabled shell

  3. Create an empty java project:

    mvn archetype:generate -DgroupId=com.example -DartifactId=guice -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  4. Switch to the guice subdirectory

  5. In the automatically generated pom.xml:

  • in the dependencies element add a dependency to guice:

    com.google.inject guice 4.1.0
  • add the following plugin to your project (allows an easy test run)

    ..... org.codehaus.mojo exec-maven-plugin 1.5.0 Main
  1. Add the classes shown above into the default package in src/main/java, each in its own file

  2. Run and enjoy

    …\guice>mvn exec:java [INFO] Scanning for projects… [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building guice 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- exec-maven-plugin:1.5.0:java (default-cli) @ guice --- Hello, world! [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.800 s [INFO] Finished at: 2016-10-09T11:44:41+03:00 [INFO] Final Memory: 10M/309M [INFO] ------------------------------------------------------------------------


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow