Skip to content

Commit

Permalink
Merge pull request #3416 from SentryMan/avaje-inject
Browse files Browse the repository at this point in the history
Add Avaje Inject Module
  • Loading branch information
jknack committed May 12, 2024
2 parents 431ba8c + 9577655 commit 8c9dfd2
Show file tree
Hide file tree
Showing 12 changed files with 392 additions and 137 deletions.
2 changes: 1 addition & 1 deletion docs/asciidoc/dependency-injection.adoc
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[]

Expand Down
136 changes: 0 additions & 136 deletions docs/asciidoc/di-avaje.adoc

This file was deleted.

121 changes: 121 additions & 0 deletions docs/asciidoc/modules/avaje-inject.adoc
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.

54 changes: 54 additions & 0 deletions modules/jooby-avaje-inject/pom.xml
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>
Loading

0 comments on commit 8c9dfd2

Please sign in to comment.