96
‹#› © 2016 Pivotal Software, Inc. All rights reserved. Spring Kotlin Toshiaki Maki (@making) [email protected] JJUG Night Seminar 2017 Feb 2017-02-20

Spring ❤️ Kotlin #jjug

Embed Size (px)

Citation preview

Page 1: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved. ‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Spring ❤ KotlinToshiaki Maki (@making) [email protected] JJUG Night Seminar 2017 Feb 2017-02-20

Page 2: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Who am I ?• Toshiaki Maki (@making) https://ik.am

•Sr. Solutions Architect @Pivotal

•Spring ☘ / Cloud Foundry ☁ / Concourse ✈ / BOSH 🐚

bit.ly/hajiboot2

Page 3: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Agenda•Spring Boot with Kotlin •Kotlin support in Spring 5

Page 4: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Spring Boot with Kotlin

Page 5: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Spring Initializr

Page 6: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Spring Initializr

Page 7: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 8: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 9: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 10: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 11: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 12: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 13: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 14: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

package com.example import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.SpringBootApplication@SpringBootApplication class DemoApplication fun main(args: Array<String>) { SpringApplication.run(DemoApplication::class.java, *args) }

Page 15: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 16: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

package com.example import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RestController @RestController class HelloController { @GetMapping("/") fun hello() = "Hello World!" }

Page 17: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

package com.example import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RestController @RestController class HelloController { @GetMapping("/") fun hello() = "Hello World!" }

Page 18: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

// Kotlinopen class AppConfig { @Bean open fun restTemplate() = RestTemplate() }

Page 19: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

// Kotlinopen class AppConfig { @Bean open fun restTemplate() = RestTemplate() }

👇

👇

Page 20: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

// Kotlinopen class AppConfig { @Bean open fun restTemplate() = RestTemplate() }

👇

👇

😩

Page 21: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Cglib and Spring// Javaclass AppConfig { @Bean RestTemplate restTemplate() { return new RestTemplate(); } }

Page 22: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Cglib and Spring// Javaclass AppConfig { @Bean RestTemplate restTemplate() { return new RestTemplate(); } }

Singleton

Page 23: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Cglib and Spring (Pseudo Code)class AppConfig$$ extends AppConfig { @Override RestTemplate restTemplate() { if (context.contains("restTemplate")) { return context.get("restTemplate"); } return super.restTemplate(); } }

Page 24: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Cglib and Spring (Pseudo Code)class AppConfig$$ extends AppConfig { @Override RestTemplate restTemplate() { if (context.contains("restTemplate")) { return context.get("restTemplate"); } return super.restTemplate(); } }

Methods in Kotlin are final by default !!

open removes final

Page 25: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

<plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>${kotlin.version}</version> <configuration> <compilerPlugins> <plugin>spring</plugin> </compilerPlugins> </configuration></plugin>

Page 26: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

open class AppConfig { @Bean open fun restTemplate() = RestTemplate() }

Page 27: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

open class AppConfig { @Bean open fun restTemplate() = RestTemplate() }

Page 28: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

class AppConfig { @Bean fun restTemplate() = RestTemplate() }

Page 29: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

class AppConfig { @Bean fun restTemplate() = RestTemplate() }

😍

Page 30: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

http://start.spring.io/#!language=kotlin

Page 31: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Kotlin support in Spring 5

Page 32: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Spring Framework 5•Java 8 baseline, Java 9 compatibility •Reactive Support & Spring WebFlux •Router Functions •Performance improvements •HTTP/2 support •Kotlin support

Page 33: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Kotlin Support

Page 34: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Kotlin Support

Page 35: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Kotlin Support•Extension Functions •Reified type parameters

Page 36: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Kotlin Support•Extension Functions •Reified type parameters

🤔

Page 37: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

package com.example;

public class Foo { public <T> T create(Class<T> clazz) { try { return clazz.newInstance(); } catch (Exception e) { throw new IllegalStateException(e); } }}

Page 38: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

// JavaFoo foo = new Foo();Bar bar = foo.create(Bar.class);

Page 39: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

// JavaFoo foo = new Foo();Bar bar = foo.create(Bar.class);

// Kotlinval foo = Foo()val bar = foo.create(Bar::class.java)

Page 40: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

// JavaFoo foo = new Foo();Bar bar = foo.create(Bar.class);

// Kotlinval foo = Foo()val bar = foo.create(Bar::class.java)

👇

Page 41: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

KClass

Bar::class // kotlin.reflect.KClassBar::class.java // java.lang.Class

Page 42: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Extension Functions•Extends a class with new functionality without having to inherit from the class •https://kotlinlang.org/docs/reference/extensions.html#extension-functions

Page 43: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

// Kotlinval foo = Foo()val bar = foo.create(Bar::class.java)

Page 44: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

// Kotlinval foo = Foo()val bar = foo.create(Bar::class.java)

Page 45: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

// Kotlinval foo = Foo()val bar = foo.create(Bar::class)

Page 46: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Extension Functions

// Kotlinval foo = Foo()val bar = foo.create(Bar::class)

Page 47: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Extension Functionspackage com.example

import kotlin.reflect.KClass

fun <T : Any> Foo.create(kclass: KClass<T>) = create(kclass.java)

FooExtensions.kt

Page 48: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Extension Functionspackage com.example

import kotlin.reflect.KClass

fun <T : Any> Foo.create(kclass: KClass<T>) = create(kclass.java)👇

FooExtensions.kt

Page 49: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Extension Functions

// Kotlinval foo = Foo()val bar = foo.create(Bar::class)

😍

Page 50: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Reified type parameters •Access a type passed to us as a parameter •https://kotlinlang.org/docs/reference/inline-functions.html#reified-type-parameters

Page 51: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Reified type parameters

inline fun <reified T : Any> Foo.create() = create(T::class.java)

Page 52: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Reified type parameters

inline fun <reified T : Any> Foo.create() = create(T::class.java) 👇

👇

Page 53: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

val foo = Foo()val bar = foo.create(Bar::class)

Page 54: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

val foo = Foo()val bar = foo.create(Bar::class)

Page 55: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Reified type parameters

val foo = Foo()val bar = foo.create<Bar>()

😍

Page 56: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Reified type parameters

val foo = Foo()val bar: Bar = foo.create()

😍

Page 57: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Reified type parameters

val foo = Foo()val bar: Bar = foo.create()

😍

"idiomatic Kotlin code"

Page 58: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

"idiomatic Kotlin code" with Spring 5

Page 59: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Application Context

// JavaApplicationContext context = ...;Bar bar = context.getBean(Bar.class);

Page 60: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Application Context (Extension)

// Kotlinval context = ...val bar = context.getBean(Bar::class)

Page 61: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Application Context (Reified Type)

// Kotlinval context = ...val bar = context.getBean<Bar>()

Page 62: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Application Context (Reified Type)

// Kotlinval context = ...val bar: Bar = context.getBean()

Page 63: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

JdbcTemplate

// JavaLong count = jdbcTemplate .queryForObject("SELECT count(*) FROM foo" , Long.class);

Page 64: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

JdbcTemplate (Extension)

// Kotlinval count = jdbcTemplate .queryForObject("SELECT count(*) FROM foo" , Long::class)

Page 65: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

JdbcTemplate (Reified Type)

// Kotlinval count = jdbcTemplate .queryForObject<Long>( "SELECT count(*) FROM foo")

Page 66: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

JdbcTemplate (Reified Type)

// Kotlinval count: Long = jdbcTemplate .queryForObject("SELECT count(*) FROM foo")

Page 67: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

RestTemplate

// JavaString foo = restTemplate .getForObject("http://abc.io",String.class);

Page 68: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

RestTemplate (Reified Type)

// Kotlinval foo = restTemplate .getForObject<String>("http://abc.io");

Page 69: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

RestTemplate (Reified Type)

// Kotlinval foo: String = restTemplate .getForObject("http://abc.io");

Page 70: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

RestTemplate

// JavaList<Foo> foos = restTemplate .getForObject("http://abc.io", List<Foo>.class);

Page 71: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

RestTemplate

// JavaList<Foo> foos = restTemplate .getForObject("http://abc.io", List<Foo>.class);

Compile Error!

Page 72: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

RestTemplate

// JavaList<Foo> foos = restTemplate .exchange("http://abc.io", HttpMethod.GET, null, new ParameterizedTypeReference<List<Foo>>() {}).getBody();

Page 73: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

RestTemplate

// JavaList<Foo> foos = restTemplate .exchange("http://abc.io", HttpMethod.GET, null, new ParameterizedTypeReference<List<Foo>>() {}).getBody();

💩

Page 74: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

RestTemplate (Reified Type)

Page 75: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

RestTemplate (Reified Type)

// Kotlinval foos: List<Foo> = restTemplate .getForObject("http://abc.io");

Page 76: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

RestTemplate (Reified Type)

// Kotlinval foos: List<Foo> = restTemplate .getForObject("http://abc.io");

😍

Page 77: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Spring ❤ Kotlin

Page 78: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Spring Framework 5•Java 8 baseline, Java 9 compatibility •Reactive Support & Spring WebFlux •Router Functions •Performance improvements •HTTP/2 support •Kotlin support

Page 79: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Spring Framework 5•Java 8 baseline, Java 9 compatibility •Reactive Support & Spring WebFlux •Router Functions •Performance improvements •HTTP/2 support •Kotlin support

👇👇

Page 80: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Spring Framework 5•Java 8 baseline, Java 9 compatibility •Reactive Support & Spring WebFlux •Router Functions •Performance improvements •HTTP/2 support •Kotlin support

👇👇 🤔

Page 81: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 82: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

WebMVC + @Controller in Java@RestControllerpublic class UserController { private final UserRepository repo; UserController(UserRepository repo) {/.../} @GetMapping List<User> users() { return repo.findAll(); }}

Page 83: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

WebFlux + @Controller in Java@RestControllerpublic class UserController { private final ReactiveUserRepository repo; UserController(ReactiveUserRepository repo) {/.../} @GetMapping Flux<User> users() { return repo.findAll(); }}

Page 84: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

WebFlux + @Controller in Java@RestControllerpublic class UserController { private final ReactiveUserRepository repo; UserController(ReactiveUserRepository repo) {/.../} @GetMapping Flux<User> users() { return repo.findAll(); }}

Non-Blocking!!

Page 85: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

WebFlux + RouterFunction in Java

Page 86: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

WebFlux + RouterFunction in Java

RouterFunction<?> routes = route(GET("/users"), req -> ok() .body(repo.findAll(), User.class));

Page 87: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

WebFlux + RouterFunction in Kotlin

{ accept(APPLICATION_JSON).apply { GET("/users",{ ok().body(fromPublisher(repo.findAll()) }) }}

Page 88: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Check source code!!

• https://github.com/mix-it/mixit

• https://github.com/making/demo-router-functions

Page 89: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Other Kotlin Support•Functional Bean Registration with Kotlin •WebFlux functional API, the Kotlin way •Leveraging Kotlin nullable information •Kotlin Script based templates •... •https://speakerdeck.com/sdeleuze/functional-

web-applications-with-kotlin-and-spring-5?

Page 90: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Release date

https://jira.spring.io/browse/SPR

Page 91: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

http://start.spring.io/#!language=kotlin

Page 92: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

http://start.spring.io/#!language=kotlin

Page 93: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

http://start.spring.io/#!language=kotlin

Page 94: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

http://start.spring.io/#!language=kotlin

Page 95: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Resources• https://spring.io/blog/2017/01/04/introducing-kotlin-support-

in-spring-framework-5-0

• https://speakerdeck.com/sdeleuze

• https://blog.ik.am/entries/407

Page 96: Spring ❤️ Kotlin #jjug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

CfP for JJUG CCC 2017 Spring •Submit Call for Paper!!! 🙇 •http://www.java-users.jp/?p=2830