1. Spring
1) Add Spring dependency to your project:
Maven
Gradle
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-spring</artifactId>
<version>2.16.2</version>
</dependency>
2) Install Spring:
Installing Spring
Java
Kotlin
package myapp; (1)
import static io.jooby.Jooby.runApp;
import io.jooby.di.SpringModule;
public class App extends Jooby {
{
install(new SpringModule()); (2)
get("/", ctx -> {
MyService service = require(MyService.class); (3)
return service.doSomething();
});
}
public static void main(String[] args) {
runApp(args, App::new);
}
}
| 1 | Spring scan the package myapp and subpackages |
| 2 | Install Spring module |
| 3 | The require(Class) call is now resolved by Spring |
Spring uses the application package and sub-packages to scan. If you need extra packages, provide them at creation time:
install(new Spring("foo", "bar"));
1.1. Property Injection
Configuration properties can be injected using the @Value annotation:
application.conf
currency = USD
Java
Kotlin
import javax.injext.Inject;
import org.springframework.beans.factory.annotation.Value;
public class BillingService {
@Inject
public BillingService(@Value("currency") String currency) {
...
}
}
1.2. MVC routes
The Spring extension does a bit more in relation to MVC routes:
-
A MVC route annotated with the
org.springframework.stereotype.Controllerannotation is automatically registered. No need to register it manually -
A MVC route provided by Spring is a singleton object by default. Singleton is the default scope in Spring
MVC route
Java
Kotlin
import org.springframework.stereotype.Controller;
@Controller
public class Hello {
@GET
public String sayHi() {
return "Hi Spring!";
}
}