Skip to content

Latest commit

 

History

History
130 lines (98 loc) · 2.2 KB

weld.adoc

File metadata and controls

130 lines (98 loc) · 2.2 KB

Weld

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()
    }
  }
}
  1. Install Weld

  2. The javadoc:Jooby[require, java.lang.Class] call is now resolved by Weld

Property Injection

Configuration properties can be injected using the @Named annotation:

application.conf
currency = USD
Java
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) {
  ...
}

MVC routes

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 the javax.inject.Singleton annotation

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!"
   }
}