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

Add Avaje Inject Module #3416

Merged
merged 12 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
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
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
Loading