-
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3416 from SentryMan/avaje-inject
Add Avaje Inject Module
- Loading branch information
Showing
12 changed files
with
392 additions
and
137 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
== Dependency Injection | ||
|
||
include::di-avaje.adoc[] | ||
include::modules/avaje-inject.adoc[] | ||
|
||
include::di-dagger.adoc[] | ||
|
||
|
This file was deleted.
Oops, something went wrong.
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,121 @@ | ||
=== Avaje Inject | ||
|
||
1) Add Avaje Inject to your project | ||
|
||
[dependency, groupId="io.jooby", artifactId="jooby-avaje-inject", version="1.1.0"] | ||
. | ||
|
||
|
||
2) Install Avaje Inject: | ||
|
||
.Installing Avaje Inject | ||
[source,java,role = "primary"] | ||
---- | ||
public class App extends Jooby { | ||
{ | ||
install(AvajeInjectModule.of()); <1> | ||
get("/", ctx -> { | ||
MyService service = require(MyService.class); <2> | ||
return service.doSomething(); | ||
}); | ||
} | ||
public static void main(String[] args) { | ||
runApp(args, App::new); | ||
} | ||
} | ||
---- | ||
|
||
.Kotlin | ||
[source, kotlin, role = "secondary"] | ||
---- | ||
fun main(args: Array<String>) { | ||
runApp(args) { | ||
install(AvajeInjectModule.of()) <1> | ||
get ("/") { | ||
val service = require(MyService::class) <2> | ||
service.doSomething() | ||
} | ||
} | ||
} | ||
---- | ||
|
||
<1> Install Avaje Inject module | ||
<2> The javadoc:Jooby[require, java.lang.Class] call is now resolved by Avaje Inject | ||
|
||
==== Property Injection | ||
|
||
Configuration properties can be injected using the `@Named` annotation. As Avaje checks beans at compile time, `@InjectModule(requires={String.class})`: | ||
|
||
.application.conf | ||
[source, bash] | ||
---- | ||
currency = USD | ||
---- | ||
|
||
.Java | ||
[source,java,role="primary"] | ||
---- | ||
@Singleton | ||
public class BillingService { | ||
@Inject | ||
public BillingService(@Named("currency") String currency) { | ||
... | ||
} | ||
} | ||
---- | ||
|
||
.Kotlin | ||
[source,kotlin,role="secondary"] | ||
---- | ||
@Singleton | ||
class BillingService @Inject constructor(@Named("currency") currency: String) { | ||
... | ||
} | ||
---- | ||
|
||
==== MVC routes | ||
|
||
Avaje Inject will also provisioning MVC routes | ||
|
||
.MVC and Avaje Inject | ||
[source,java,role = "primary"] | ||
---- | ||
public class App extends Jooby { | ||
{ | ||
install(AvajeInjectModule.of()); <1> | ||
mvc(MyController.class); <2> | ||
} | ||
public static void main(String[] args) { | ||
runApp(args, App::new); | ||
} | ||
} | ||
---- | ||
|
||
.Kotlin | ||
[source, kotlin, role = "secondary"] | ||
---- | ||
fun main(args: Array<String>) { | ||
runApp(args) { | ||
install(AvajeInjectModule.of()) <1> | ||
mvc(MyController::class) <2> | ||
} | ||
} | ||
---- | ||
|
||
<1> Install Avaje Inject module | ||
<2> Register a MVC route | ||
|
||
The lifecycle of `MyController` is now managed by Avaje Inject. | ||
|
||
In Avaje Inject, the dependency graph is typically validated when the application compiles. As beans provided by Jooby Modules are registered at runtime, you must add `@InjectModules(requires={String.class, <other Jooby Module Provided Bean ClassNames>...})` to inform the avaje processor that these beans are provided at runtime. | ||
|
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,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
|
||
<parent> | ||
<groupId>io.jooby</groupId> | ||
<artifactId>modules</artifactId> | ||
<version>3.1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>jooby-avaje-inject</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.jooby</groupId> | ||
<artifactId>jooby</artifactId> | ||
<version>${jooby.version}</version> | ||
</dependency> | ||
|
||
<!-- Avaje Inject --> | ||
<dependency> | ||
<groupId>io.avaje</groupId> | ||
<artifactId>avaje-inject</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.avaje</groupId> | ||
<artifactId>avaje-inject-generator</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- Test dependencies --> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.jacoco</groupId> | ||
<artifactId>org.jacoco.agent</artifactId> | ||
<classifier>runtime</classifier> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
Oops, something went wrong.