generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
191ddc5
commit e10346f
Showing
16 changed files
with
210 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...st/java/io/micronaut/guice/doc/examples/bindings/annotations2/BindingAnnotation2Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.micronaut.guice.doc.examples.bindings.annotations2; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
|
||
import io.micronaut.core.annotation.Introspected; | ||
import io.micronaut.guice.annotation.Guice; | ||
import io.micronaut.guice.doc.examples.bindings.annotations2.impl.CheckoutCreditCardProcessor; | ||
import io.micronaut.guice.doc.examples.bindings.annotations2.impl.CreditCardProcessor; | ||
import io.micronaut.guice.doc.examples.bindings.annotations2.impl.CreditCardProcessorModule; | ||
import io.micronaut.guice.doc.examples.bindings.annotations2.impl.GoogleCheckout; | ||
import io.micronaut.guice.doc.examples.bindings.annotations2.impl.PayPal; | ||
import io.micronaut.guice.doc.examples.bindings.annotations2.impl.PayPalCreditCardProcessor; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import jakarta.inject.Inject; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@MicronautTest(startApplication = false) | ||
@Guice( | ||
modules = CreditCardProcessorModule.class, | ||
packages = "io.micronaut.guice.doc.examples.bindings.annotations2.impl" | ||
) | ||
@Introspected(classes = PayPalCreditCardProcessor.class) | ||
class BindingAnnotation2Test { | ||
@Inject @PayPal | ||
CreditCardProcessor paypalProcessor; | ||
@Inject @GoogleCheckout | ||
CreditCardProcessor checkoutProcessor; | ||
|
||
@Test | ||
void testInjectWithQualifiers() { | ||
assertInstanceOf(CheckoutCreditCardProcessor.class, checkoutProcessor); | ||
assertInstanceOf(PayPalCreditCardProcessor.class, paypalProcessor); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
.../micronaut/guice/doc/examples/bindings/annotations2/impl/CheckoutCreditCardProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package io.micronaut.guice.doc.examples.bindings.annotations2.impl; | ||
|
||
public class CheckoutCreditCardProcessor implements CreditCardProcessor { | ||
} |
4 changes: 4 additions & 0 deletions
4
.../java/io/micronaut/guice/doc/examples/bindings/annotations2/impl/CreditCardProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package io.micronaut.guice.doc.examples.bindings.annotations2.impl; | ||
|
||
public interface CreditCardProcessor { | ||
} |
24 changes: 24 additions & 0 deletions
24
...io/micronaut/guice/doc/examples/bindings/annotations2/impl/CreditCardProcessorModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.micronaut.guice.doc.examples.bindings.annotations2.impl; | ||
|
||
// tag::class[] | ||
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; | ||
} | ||
} | ||
// end::class[] |
16 changes: 16 additions & 0 deletions
16
.../test/java/io/micronaut/guice/doc/examples/bindings/annotations2/impl/GoogleCheckout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.micronaut.guice.doc.examples.bindings.annotations2.impl; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import com.google.inject.BindingAnnotation; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
|
||
@BindingAnnotation | ||
@Target({ FIELD, PARAMETER, METHOD }) | ||
@Retention(RUNTIME) | ||
public @interface GoogleCheckout {} |
15 changes: 15 additions & 0 deletions
15
...uice/src/test/java/io/micronaut/guice/doc/examples/bindings/annotations2/impl/PayPal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.micronaut.guice.doc.examples.bindings.annotations2.impl; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import jakarta.inject.Qualifier; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
@Qualifier | ||
@Target({ FIELD, PARAMETER, METHOD }) | ||
@Retention(RUNTIME) | ||
public @interface PayPal {} |
4 changes: 4 additions & 0 deletions
4
...io/micronaut/guice/doc/examples/bindings/annotations2/impl/PayPalCreditCardProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package io.micronaut.guice.doc.examples.bindings.annotations2.impl; | ||
|
||
public class PayPalCreditCardProcessor implements CreditCardProcessor { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
TODO | ||
This module allows importing https://github.com/google/guice[Guice] modules into a Micronaut application making the Guice bindings available for Dependency Injection. | ||
|
||
Note that only a subset of the Guice API (primarily what is implemented is the https://google.github.io/guice/api-docs/latest/javadoc/com/google/inject/Binder.html[Guice Binding EDSL]) and the following features are not supported: | ||
|
||
* Custom Guice Scopes (other than Singleton) 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
Micronaut is based on implicit binding 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 https://google.github.io/guice/api-docs/latest/javadoc/com/google/inject/Binder.html[Guice Binding EDSL]). | ||
|
||
This integration allows you to import modules written for Guice and use them in a Micronaut application. | ||
|
||
By declaring the ann:guice.annotation.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 | ||
snippet::io.micronaut.guice.doc.examples.bindings.annotations.CreditCardProcessorModule[tags="class", indent=0, project="micronaut-guice"] | ||
|
||
You can declare: | ||
|
||
[source,java] | ||
---- | ||
@Guice(modules = CreditCardProcessorModule.class) | ||
---- | ||
|
||
If the module has constructor parameter these will need to also be https://docs.micronaut.io/latest/guide/#beans[declared as beans] or https://docs.micronaut.io/latest/guide/#beanImport[imported]. | ||
|
||
TIP: 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: | ||
|
||
[source,java] | ||
---- | ||
@Guice( | ||
modules = CreditCardProcessorModule.class, | ||
classes = PayPalCreditCardProcessor.class | ||
) | ||
---- | ||
|
||
TIP: To import multiple classes for injection use 'packages'. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
TODO | ||
To use this module add the following annotation processor: | ||
|
||
dependency:io.micronaut.guice:micronaut-guice-processor[scope="annotationProcessor"] | ||
|
||
Then add the `micronaut-guice` module: | ||
|
||
dependency:io.micronaut.guice:micronaut-guice[scope="compile"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters