1) Add Weld dependency to your project:
.
2) Install Weld:
Installing Weld
import io.jooby.di.WeldModule;
import static io.jooby.Jooby.runApp;
public class App extends Jooby {
{
install(new WeldModule()); (1)
get ("/", ctx -> {
MyService service = require(MyService.class); (2)
service.doSomething();
});
}
public static void main(String[] args) {
runApp(args, App::new);
}
}Kotlin
import io.jooby.di.WeldModule
import io.jooby.runApp
fun main(args: Array<String>) {
runApp(args) {
install(WeldModule()) (1)
get ("/") {
val service = require(MyService::class) (2)
service.doSomething()
}
}
}-
Install Weld
-
The javadoc:Jooby[require, java.lang.Class] call is now resolved by Weld
Configuration properties can be injected using the @Named annotation:
application.conf
currency = USDJava
import javax.injext.Inject;
import javax.injext.Named;
public class BillingService {
@Inject
public BillingService(@Named("currency") String currency) {
...
}
}Kotlin
import javax.injext.Inject
import javax.injext.Named
class BillingService @Inject constructor(@Named("currency") currency: String) {
...
}The Weld extension does a bit more in relation to MVC routes:
-
A MVC route annotated with the javadoc:annotations.Path[] annotation is automatically registered. No need to register it manually
-
The default scope is
prototype(creates a new instance per request). If you prefer a single instance add thejavax.inject.Singletonannotation
MVC route
import io.jooby.annotations.*;
@Path("/")
public class Hello {
@GET
public String sayHi() {
return "Hi Weld!";
}
}Kotlin
import io.jooby.annotations.*;
@Path("/")
class Hello {
@GET
fun sayHi(): String {
return "Hi Weld!"
}
}