Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc clarifications #6

Merged
merged 1 commit into from
May 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/main/docs/guide/modules.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,32 @@ snippet::io.micronaut.guice.doc.examples.bindings.annotations.CreditCardProcesso

You can declare:

.Using `@Guice` annotation
[source,java]
----
@Guice(modules = CreditCardProcessorModule.class)

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);
}
}
----

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].
<1> The ann:guice.annotation.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.

NOTE: If the module has constructor parameters 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:
Expand All @@ -32,5 +49,5 @@ Note that when registering bindings the target type (in the above case the `to(P
)
----

TIP: To import multiple classes for injection use 'packages'.
TIP: To import multiple Guice classes for injection use 'packages'.

Loading