Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Spring Framework 5 & Spring Boot 2.0

Spring Framework 5 & Spring Boot 2.0

Slides of the talk I gave at W-JAX 2017.

@springcentral

Oliver Drotbohm

November 09, 2017
Tweet

More Decks by Oliver Drotbohm

Other Decks in Programming

Transcript

  1. Spring Framework 5
    & Spring Boot 2.0
    / olivergierke
    Oliver Gierke ƀ [email protected]
    Modern Enterprise Java in 2018

    View Slide

  2. https://github.com/olivergierke/spring-five-functional-reactive
    Sample Code
    2

    View Slide

  3. Spring Framework 5
    3

    View Slide

  4. Infrastructure
    4

    View Slide

  5. Infrastructure
    • JavaSE 8 baseline
    • JDK 9 compatibility through automatic modules and JDK 9 base CI builds
    • JavaEE 7 baseline
    • Servlet API 3.1
    • JPA 2.1 (OpenJPA support dropped)
    • JMS 2.0
    • Bean Validation 1.1
    • Support for selected JavaEE 8 APIs
    • Servlet 4
    • Bean Validation 2.0
    • JSON Binding API
    5

    View Slide

  6. JDK 9 Support
    • Many general JVM improvements
    • Compact Strings
    • G1 by default
    • TLS protocol stack
    • Jigsaw
    • Stable automatic module names
    • We currently recommend to still run in classpath mode
    6

    View Slide

  7. HTTP/2 Support
    • Mostly via Servlet 4.0
    • HTTP/2 support in Servlet containers
    • PushBuilder API
    7

    View Slide

  8. JUnit 5
    • Test context framework support
    • for both JUnit 4 and JUnit 5
    • Dependency injection capabilities inspired by Spring
    • Constructor injection
    • Parameter injection á la Spring MVC
    • Meta annotation support inspired by Spring
    8
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(…)
    public class MyTest { … }

    View Slide

  9. JUnit 5
    • Test context framework support
    • for both JUnit 4 and JUnit 5
    • Dependency injection capabilities inspired by Spring
    • Constructor injection
    • Parameter injection á la Spring MVC
    • Meta annotation support inspired by Spring
    8
    @SpringJUnitConfig(…)
    public class MyTest { … }

    View Slide

  10. Functional

    container extensions
    9

    View Slide

  11. Functional container extensions
    10
    GenericApplicationContext ctx = new GenericApplicationContext();


    View Slide

  12. Functional container extensions
    10
    GenericApplicationContext ctx = new GenericApplicationContext();

    // Default constructor via reflection

    ctx.registerBean(First.class);


    View Slide

  13. Functional container extensions
    10
    GenericApplicationContext ctx = new GenericApplicationContext();

    // Default constructor via reflection

    ctx.registerBean(First.class);

    // Explicit constructor via Supplier

    ctx.registerBean(Second.class,

    () -> new Second(ctx.getBean(First.class)));


    View Slide

  14. Functional container extensions
    10
    GenericApplicationContext ctx = new GenericApplicationContext();

    // Default constructor via reflection

    ctx.registerBean(First.class);

    // Explicit constructor via Supplier

    ctx.registerBean(Second.class,

    () -> new Second(ctx.getBean(First.class)));

    // Explicit constructor plus BeanDefinition customization

    ctx.registerBean(Third.class,

    () -> new Third(ctx.getBean(First.class)),

    bd -> bd.setLazyInit(true));

    View Slide

  15. A quick detour
    Reactive Programming
    11

    View Slide

  16. Reactive fundamentals
    12
    Publisher
    Subscriber
    Data
    Subscribe

    View Slide

  17. Reactive fundamentals
    12
    Publisher
    Subscriber
    Data
    Subscribe

    View Slide

  18. Reactive fundamentals
    12
    Publisher
    Subscriber
    Data
    Subscribe
    Business logic

    using operators
    ?

    View Slide

  19. Reactive programming 101
    13
    Mono.just("Hello")
    .map(word -> word.concat(" World!"))
    .subscribe(System.out::println);

    Flux.just("Hello", "World")
    .flatMap(word -> Flux.fromArray(word.split("")))
    .subscribe(System.out::println);

    View Slide

  20. Reactive programming 101
    14
    someMono
    .map(word -> word.concat("World"))
    .…
    someFlux
    .flatMap(word -> Flux.fromArray(word.split("")))
    .…

    View Slide

  21. Reactive programming 101
    15
    someMono // Created by the infrastructure
    .map(word -> word.concat("World"))
    .… // Handled by the infrastructure
    someFlux // Created by the infrastructure
    .flatMap(word -> Flux.fromArray(word.split("")))
    .… // Handled by the infrastructure

    View Slide

  22. Reactive programming 101
    16
    someMono // Created by the infrastructure
    .map(word -> word.concat("World"))
    .… // Handled by the infrastructure
    someFlux // Created by the infrastructure
    .flatMap(word -> Flux.fromArray(word.split("")))
    .… // Handled by the infrastructure

    View Slide

  23. Reactive programming
    • Subscription based
    • Push VS. pull model
    • Backpressure blurs the line
    • Two-phase execution
    • First: to build up the pipeline. Second: once the data starts flowing
    • Similar to programming model of the Java 8 Stream API
    • Lazy execution
    • Nothing happens until someone subscribes
    • Backpressure
    • Subscriber signals capability to handle the amount of data
    17

    View Slide

  24. Reactive programming
    • Optimized for resource usage, scalability and stability
    • Not necessarily faster
    • Optimized for „mean time to first result“
    • Challenge: debugging and tests
    • Needs non-blocking across the entire stack
    • Usage of blocking APIs (e.g. JDBC) severely complicates the picture and limits efficiency
    18

    View Slide

  25. Spring WebFlux
    19

    View Slide

  26. Runtime stacks
    20
    Servlet container Netty, Undertow, Servlet 3.1
    Servlet API Spring Web Reactive API
    Spring MVC Spring WebFlux(.fn)
    New!
    New!

    View Slide

  27. WebFlux Controller
    21
    @Controller
    class ReactiveUserController {
    private final UserRepository users;
    // Constructor for Dependency Injection
    @GetMapping("/users")

    Flux getUsers() {

    return this.repository.findAll();

    }

    }

    View Slide

  28. WebFlux Controller
    22
    @Controller
    class ReactiveUserController {
    private final UserRepository users;
    // Constructor for Dependency Injection
    @GetMapping("/users")

    Flux getUsers() {

    return this.repository.findAll();

    }

    }

    View Slide

  29. WebFlux Controller
    23
    @Controller
    class ReactiveUserController {
    // … continued
    @PostMapping("/users")
    Mono createUser(@RequestBody Mono user) {
    return user.flatMap(this.repository::save);
    }
    @GetMapping("/users/{id}/")
    Mono getUser(@PathVariable Long id) {
    return this.repository.findById(id);
    }
    }

    View Slide

  30. WebFlux Controller
    24
    @Controller
    class ReactiveUserController {
    // … continued
    @PostMapping("/users")
    Mono createUser(@RequestBody Mono user) {
    return user.flatMap(this.repository::save);
    }
    @GetMapping("/users/{id}/")
    Mono getUser(@PathVariable Long id) {
    return this.repository.findById(id);
    }
    }

    View Slide

  31. Spring WebFlux.fn
    25

    View Slide

  32. Functional controller
    26
    @Component
    class FunctionalUserController {
    private final UserRepository repository;
    // Constructor for Dependency Injection
    Mono getUser(ServerRequest request) {
    Mono user = Mono.just(request.pathVariable("id"))
    .flatMap(this.repository::findById);
    return ServerResponse.ok().body(user, User.class);
    }
    }

    View Slide

  33. Functional controller
    27
    @Component
    class FunctionalUserController {
    private final UserRepository repository;
    // Constructor for Dependency Injection
    Mono getUser(ServerRequest request) {
    Mono user = Mono.just(request.pathVariable("id"))
    .flatMap(this.repository::findById);
    return ServerResponse.ok().body(user, User.class);
    }
    }

    View Slide

  34. Functional controller
    28
    @Component
    class FunctionalUserController {
    // … continued
    Mono getUsers(ServerRequest request) {
    Flux users = this.repository.findAll();
    return ServerResponse.ok().body(users, User.class);
    }
    }

    View Slide

  35. Router Functions
    29
    @SpringBootApplication
    class ApplicationConfiguration {
    @Bean
    RouterFunction> routes(FunctionalUserController controller) {
    return RouterFunctions
    .route(GET("/users"), controller::getUsers)
    .andRoute(GET("/users/{id}"), controller::getUser);
    }
    }

    View Slide

  36. Kotlin Extensions
    30

    View Slide

  37. ApplicationContext extensions
    31
    // In GenericApplicationContextExtension.kt in spring-context


    inline fun GenericApplicationContext.registerBean(

    vararg customizers: BeanDefinitionCustomizer,

    crossinline function: (ApplicationContext) -> T) {


    registerBean(T::class.java, Supplier { function.invoke(this) }, *customizers)

    }

    // Allows …


    context.registerBean { Foo() }

    View Slide

  38. Functional routing with Kotlin
    32
    val router = router {

    val users = …

    accept(TEXT_HTML).nest {

    "/" { ok().render("index") }

    "/sse" { ok().render("sse") }

    "/users" {

    ok().render("users", mapOf("users" to users.map { it.toDto() }))

    }

    }
    ("/api/users" and accept(APPLICATION_JSON)) {

    ok().body(users)

    }

    }

    View Slide

  39. Bean definitions – Kotlin style
    33
    val databaseContext = beans {

    bean()

    bean()

    environment( { !activeProfiles.contains("cloud") } ) {

    bean {

    CommandLineRunner { initializeDatabase(ref()) }

    }

    }
    }


    fun initializeDatabase(userRepository: UserRepository) { // ... }

    View Slide

  40. Miscellaneous
    • Component indexing for faster startup
    • APT processor included in spring-context-indexer
    • Creates index in META-INF/spring.components
    • Nullability annotations in the entire codebase
    • @Nullable to indicate optionality at injection points
    • Data binding against immutable objects
    • Via constructor argument resolution and support for Kotlin / Lombok
    • WebClient as reactive alternative to RestTemplate
    34

    View Slide

  41. Spring Boot 2.0
    35

    View Slide

  42. Core themes
    • Upgrade to Java 8 and Spring Framework 5
    • Includes upgrades to all ecosystem projects based on those versions
    • Infrastructure upgrades
    • Jetty 9.4
    • Tomcat 8.5
    • Hibernate 5.2
    • Hikari connection pool (previously Tomcat)
    • Reactive web test support
    • OAuth 2.0 support moved to Spring Security
    • Tweaked defaults
    36

    View Slide

  43. Core themes
    • Actuator refactorings
    • Resources moved to /application/…
    • All secured by default
    • Micrometer support
    • Actuator customization API
    • To abstract over Spring MVC, Spring WebFlux, JAX-RS and JMX
    37

    View Slide

  44. Pruning
    • General deprecations present in 1.5
    • CRaSH project, Spring Loaded
    38

    View Slide

  45. Related talks
    10:45 - 11:45 — Going reactive with Spring Data
    Jens Schauder (Spring Data team member), Raum: Partenkirchen
    12:00 - 13:00 — The Beginner’s Guide to Spring Cloud
    Spencer Gibb (Spring Cloud co-lead), Raum: Partenkirchen
    39

    View Slide

  46. Thank you!
    Questions?
    / olivergierke
    Oliver Gierke ƀ [email protected]

    View Slide

  47. Resources
    41

    View Slide

  48. Resources
    Spring Framework 5 – Migration guide
    https://github.com/spring-projects/spring-framework/wiki/What's-New-in-Spring-Framework-5.x
    https://github.com/spring-projects/spring-framework/wiki/Upgrading-to-Spring-Framework-5.x
    Spring Framework 5 – FAQ
    https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-5-FAQ
    42

    View Slide

  49. Resources
    Blog series on Reactive Programming
    https://spring.io/blog/2016/06/07/notes-on-reactive-programming-part-i-the-reactive-landscape
    Reactor – Project homepage
    https://projectreactor.io/
    43

    View Slide

  50. Resources
    Spring Boot 2.0 release notes
    https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Release-Notes
    Spring Boot 1.5 -> 2.0 migration guide
    https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide
    Micrometer documentation
    http://micrometer.io/docs
    Documentation of Spring Boot metrics
    https://docs.spring.io/spring-boot/docs/2.0.x/reference/htmlsingle/#production-ready-metrics
    Spring Boot 2.0 Actuators (blog post)
    https://spring.io/blog/2017/08/22/introducing-actuator-endpoints-in-spring-boot-2-0
    44

    View Slide