Micronaut Guice

Allows Importing Guice Modules

Version: 1.1.1

1 Introduction

This module allows importing Guice modules into a Micronaut application making the Guice bindings available for Dependency Injection.

Note that only a subset of the Guice API is supported (primarily what is implemented is the Guice Binding EDSL) including:

The following features are not supported:

  • Custom Guice Scopes (other than Singleton and NO_SCOPE) are not supported

  • Guice AOP/Interceptors are not supported (use Micronaut AOP instead)

  • Guice private modules are not supported

  • Static Injection is not supported

  • Guice TypeConverters are not supported (use io.micronaut.core.convert.TypeConverter instead).

  • Guice Listeners are not supported (use io.micronaut.context.event.BeanCreatedEventListener instead.)

  • None of the com.google.inject.spi API is supported.

2 Release History

For this project, you can find a list of releases (with release notes) here:

3 Quick Start

To use this module add the following annotation processor:

annotationProcessor("io.micronaut.guice:micronaut-guice-processor")
<annotationProcessorPaths>
    <path>
        <groupId>io.micronaut.guice</groupId>
        <artifactId>micronaut-guice-processor</artifactId>
    </path>
</annotationProcessorPaths>

Then add the micronaut-guice module:

implementation("io.micronaut.guice:micronaut-guice")
<dependency>
    <groupId>io.micronaut.guice</groupId>
    <artifactId>micronaut-guice</artifactId>
</dependency>

4 Importing Guice Modules

Micronaut is based on implicit binding from compilation time metadata where bindings are resolved at runtime based on the available types. Guice uses explicit binding where in the form of modules you write bindings from one type to another.

These Guice modules typically extend com.google.inject.AbstractModule and use the Guice Binding EDSL.

This integration allows you to import modules written for Guice and use them in a Micronaut application.

By declaring the @Guice annotation on your Application class (or any class that is the logical central point or your application) you can import an existing Guice module. For example given the module:

Importing a Guice Module
import com.google.inject.AbstractModule;
import com.google.inject.Provides;

public final class CreditCardProcessorModule extends AbstractModule {
    @Override
    protected void configure() {
        // This uses the optional `annotatedWith` clause in the `bind()` statement
        bind(CreditCardProcessor.class)
            .annotatedWith(PayPal.class)
            .to(PayPalCreditCardProcessor.class);
    }

    // This uses binding annotation with a @Provides method
    @Provides
    @GoogleCheckout
    public CreditCardProcessor provideCheckoutProcessor(
        CheckoutCreditCardProcessor processor) {
        return processor;
    }
}

You can declare:

Using @Guice annotation
package com.example;

import io.micronaut.runtime.Micronaut;

@Guice(modules = CreditCardProcessorModule.class) (1)
public class Application {

    public static void main(String[] args) {
        Micronaut.run(Application.class, args);
    }
}
1 The @Guice annotation is used to include the CreditCardProcessorModule.

With the above code in place you can dependency inject any types declared in the Guice module’s bindings into your own code.

If the module has constructor parameters these will need to also be declared as beans or imported.
If you want the modules only imported for a particular environment (like TEST or DEVELOPMENT) using the environments member of the @Guice annotation.

You can register one or more modules. The order the modules are installed is dictated by the order of the modules array in the annotation.

Note that when registering bindings the target type (in the above case the to(PayPalCreditCardProcessor.class) declaration) must itself be a bean that is available since Micronaut will not reflectively instantiate the type on demand like Guice does. Hence you may also need to declare the classes member:

@Guice(
    modules = CreditCardProcessorModule.class,
    classes = PayPalCreditCardProcessor.class
)
To import multiple Guice classes for injection use 'packages'.

5 Repository

You can find the source code of this project in this repository: