Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
‹#›© 2016 Pivotal Software, Inc. All rights reserved. ‹#›© 2016 Pivotal Software, Inc. All rights reserved.
Spring ❤ Kotli...
© 2016 Pivotal Software, Inc. All rights reserved.
Who am I ?
• Toshiaki Maki (@making) https://ik.am
• Sr. Solutions Arch...
© 2016 Pivotal Software, Inc. All rights reserved.
Agenda
•Spring Boot with Kotlin
•Kotlin support in Spring 5
‹#›© 2016 Pivotal Software, Inc. All rights reserved.
Spring Boot with Kotlin
© 2016 Pivotal Software, Inc. All rights reserved.
Spring Initializr
© 2016 Pivotal Software, Inc. All rights reserved.
Spring Initializr
© 2016 Pivotal Software, Inc. All rights reserved.
© 2016 Pivotal Software, Inc. All rights reserved.
© 2016 Pivotal Software, Inc. All rights reserved.
© 2016 Pivotal Software, Inc. All rights reserved.
© 2016 Pivotal Software, Inc. All rights reserved.
© 2016 Pivotal Software, Inc. All rights reserved.
© 2016 Pivotal Software, Inc. All rights reserved.
© 2016 Pivotal Software, Inc. All rights reserved.
package com.example



import org.springframework.boot.SpringApplicatio...
© 2016 Pivotal Software, Inc. All rights reserved.
© 2016 Pivotal Software, Inc. All rights reserved.
package com.example



import org.springframework.web.bind.annotation.G...
© 2016 Pivotal Software, Inc. All rights reserved.
package com.example



import org.springframework.web.bind.annotation.G...
© 2016 Pivotal Software, Inc. All rights reserved.
// Kotlin
open class AppConfig {

@Bean

open fun restTemplate() = Rest...
© 2016 Pivotal Software, Inc. All rights reserved.
// Kotlin
open class AppConfig {

@Bean

open fun restTemplate() = Rest...
© 2016 Pivotal Software, Inc. All rights reserved.
// Kotlin
open class AppConfig {

@Bean

open fun restTemplate() = Rest...
© 2016 Pivotal Software, Inc. All rights reserved.
Cglib and Spring
// Java
class AppConfig {

@Bean

RestTemplate restTem...
© 2016 Pivotal Software, Inc. All rights reserved.
Cglib and Spring
// Java
class AppConfig {

@Bean

RestTemplate restTem...
© 2016 Pivotal Software, Inc. All rights reserved.
Cglib and Spring (Pseudo Code)
class AppConfig$$ extends AppConfig {
@O...
© 2016 Pivotal Software, Inc. All rights reserved.
Cglib and Spring (Pseudo Code)
class AppConfig$$ extends AppConfig {
@O...
© 2016 Pivotal Software, Inc. All rights reserved.
<plugin>

<artifactId>kotlin-maven-plugin</artifactId>

<groupId>org.je...
© 2016 Pivotal Software, Inc. All rights reserved.
open class AppConfig {

@Bean

open fun restTemplate() = RestTemplate()...
© 2016 Pivotal Software, Inc. All rights reserved.
open class AppConfig {

@Bean

open fun restTemplate() = RestTemplate()...
© 2016 Pivotal Software, Inc. All rights reserved.
class AppConfig {

@Bean

fun restTemplate() = RestTemplate()

}
© 2016 Pivotal Software, Inc. All rights reserved.
class AppConfig {

@Bean

fun restTemplate() = RestTemplate()

}
😍
© 2016 Pivotal Software, Inc. All rights reserved.
http://start.spring.io/#!language=kotlin
‹#›© 2016 Pivotal Software, Inc. All rights reserved.
Kotlin support in Spring 5
© 2016 Pivotal Software, Inc. All rights reserved.
Spring Framework 5
•Java 8 baseline, Java 9 compatibility
•Reactive Sup...
© 2016 Pivotal Software, Inc. All rights reserved.
Kotlin Support
© 2016 Pivotal Software, Inc. All rights reserved.
Kotlin Support
© 2016 Pivotal Software, Inc. All rights reserved.
Kotlin Support
•Extension Functions
•Reified type parameters
© 2016 Pivotal Software, Inc. All rights reserved.
Kotlin Support
•Extension Functions
•Reified type parameters
🤔
© 2016 Pivotal Software, Inc. All rights reserved.
package com.example;
public class Foo {
public <T> T create(Class<T> cl...
© 2016 Pivotal Software, Inc. All rights reserved.
// Java
Foo foo = new Foo();
Bar bar = foo.create(Bar.class);
© 2016 Pivotal Software, Inc. All rights reserved.
// Java
Foo foo = new Foo();
Bar bar = foo.create(Bar.class);
// Kotlin...
© 2016 Pivotal Software, Inc. All rights reserved.
// Java
Foo foo = new Foo();
Bar bar = foo.create(Bar.class);
// Kotlin...
© 2016 Pivotal Software, Inc. All rights reserved.
KClass
Bar::class // kotlin.reflect.KClass
Bar::class.java // java.lang...
© 2016 Pivotal Software, Inc. All rights reserved.
Extension Functions
•Extends a class with new functionality
without hav...
© 2016 Pivotal Software, Inc. All rights reserved.
// Kotlin
val foo = Foo()
val bar = foo.create(Bar::class.java)
© 2016 Pivotal Software, Inc. All rights reserved.
// Kotlin
val foo = Foo()
val bar = foo.create(Bar::class.java)
© 2016 Pivotal Software, Inc. All rights reserved.
// Kotlin
val foo = Foo()
val bar = foo.create(Bar::class)
© 2016 Pivotal Software, Inc. All rights reserved.
Extension Functions
// Kotlin
val foo = Foo()
val bar = foo.create(Bar:...
© 2016 Pivotal Software, Inc. All rights reserved.
Extension Functions
package com.example
import kotlin.reflect.KClass
fu...
© 2016 Pivotal Software, Inc. All rights reserved.
Extension Functions
package com.example
import kotlin.reflect.KClass
fu...
© 2016 Pivotal Software, Inc. All rights reserved.
Extension Functions
// Kotlin
val foo = Foo()
val bar = foo.create(Bar:...
© 2016 Pivotal Software, Inc. All rights reserved.
Reified type parameters
•Access a type passed to us as a parameter
•htt...
© 2016 Pivotal Software, Inc. All rights reserved.
Reified type parameters
inline fun <reified T : Any> Foo.create()
= cre...
© 2016 Pivotal Software, Inc. All rights reserved.
Reified type parameters
inline fun <reified T : Any> Foo.create()
= cre...
© 2016 Pivotal Software, Inc. All rights reserved.
val foo = Foo()
val bar = foo.create(Bar::class)

© 2016 Pivotal Software, Inc. All rights reserved.
val foo = Foo()
val bar = foo.create(Bar::class)

© 2016 Pivotal Software, Inc. All rights reserved.
Reified type parameters
val foo = Foo()
val bar = foo.create<Bar>()

😍
© 2016 Pivotal Software, Inc. All rights reserved.
Reified type parameters
val foo = Foo()
val bar: Bar = foo.create()

😍
© 2016 Pivotal Software, Inc. All rights reserved.
Reified type parameters
val foo = Foo()
val bar: Bar = foo.create()

😍
...
© 2016 Pivotal Software, Inc. All rights reserved.
"idiomatic Kotlin code" with Spring 5
© 2016 Pivotal Software, Inc. All rights reserved.
Application Context
// Java
ApplicationContext context = ...;
Bar bar =...
© 2016 Pivotal Software, Inc. All rights reserved.
Application Context (Extension)
// Kotlin
val context = ...
val bar = c...
© 2016 Pivotal Software, Inc. All rights reserved.
Application Context (Reified Type)
// Kotlin
val context = ...
val bar ...
© 2016 Pivotal Software, Inc. All rights reserved.
Application Context (Reified Type)
// Kotlin
val context = ...
val bar:...
© 2016 Pivotal Software, Inc. All rights reserved.
JdbcTemplate
// Java
Long count = jdbcTemplate
.queryForObject("SELECT ...
© 2016 Pivotal Software, Inc. All rights reserved.
JdbcTemplate (Extension)
// Kotlin
val count = jdbcTemplate
.queryForOb...
© 2016 Pivotal Software, Inc. All rights reserved.
JdbcTemplate (Reified Type)
// Kotlin
val count = jdbcTemplate
.queryFo...
© 2016 Pivotal Software, Inc. All rights reserved.
JdbcTemplate (Reified Type)
// Kotlin
val count: Long = jdbcTemplate
.q...
© 2016 Pivotal Software, Inc. All rights reserved.
RestTemplate
// Java
String foo = restTemplate
.getForObject("http://ab...
© 2016 Pivotal Software, Inc. All rights reserved.
RestTemplate (Reified Type)
// Kotlin
val foo = restTemplate
.getForObj...
© 2016 Pivotal Software, Inc. All rights reserved.
RestTemplate (Reified Type)
// Kotlin
val foo: String = restTemplate
.g...
© 2016 Pivotal Software, Inc. All rights reserved.
RestTemplate
// Java
List<Foo> foos = restTemplate
.getForObject("http:...
© 2016 Pivotal Software, Inc. All rights reserved.
RestTemplate
// Java
List<Foo> foos = restTemplate
.getForObject("http:...
© 2016 Pivotal Software, Inc. All rights reserved.
RestTemplate
// Java
List<Foo> foos = restTemplate
.exchange("http://ab...
© 2016 Pivotal Software, Inc. All rights reserved.
RestTemplate
// Java
List<Foo> foos = restTemplate
.exchange("http://ab...
© 2016 Pivotal Software, Inc. All rights reserved.
RestTemplate (Reified Type)
© 2016 Pivotal Software, Inc. All rights reserved.
RestTemplate (Reified Type)
// Kotlin
val foos: List<Foo> = restTemplat...
© 2016 Pivotal Software, Inc. All rights reserved.
RestTemplate (Reified Type)
// Kotlin
val foos: List<Foo> = restTemplat...
© 2016 Pivotal Software, Inc. All rights reserved.
Spring ❤ Kotlin
© 2016 Pivotal Software, Inc. All rights reserved.
Spring Framework 5
•Java 8 baseline, Java 9 compatibility
•Reactive Sup...
© 2016 Pivotal Software, Inc. All rights reserved.
Spring Framework 5
•Java 8 baseline, Java 9 compatibility
•Reactive Sup...
© 2016 Pivotal Software, Inc. All rights reserved.
Spring Framework 5
•Java 8 baseline, Java 9 compatibility
•Reactive Sup...
© 2016 Pivotal Software, Inc. All rights reserved.
© 2016 Pivotal Software, Inc. All rights reserved.
WebMVC + @Controller in Java
@RestController
public class UserControlle...
© 2016 Pivotal Software, Inc. All rights reserved.
WebFlux + @Controller in Java
@RestController
public class UserControll...
© 2016 Pivotal Software, Inc. All rights reserved.
WebFlux + @Controller in Java
@RestController
public class UserControll...
© 2016 Pivotal Software, Inc. All rights reserved.
WebFlux + RouterFunction in Java
© 2016 Pivotal Software, Inc. All rights reserved.
WebFlux + RouterFunction in Java
RouterFunction<?> routes =
route(GET("...
© 2016 Pivotal Software, Inc. All rights reserved.
WebFlux + RouterFunction in Kotlin
{
accept(APPLICATION_JSON).apply {
G...
© 2016 Pivotal Software, Inc. All rights reserved.
Check source code!!
• https://github.com/mix-it/mixit
• https://github....
© 2016 Pivotal Software, Inc. All rights reserved.
Other Kotlin Support
•Functional Bean Registration with Kotlin
•WebFlux...
© 2016 Pivotal Software, Inc. All rights reserved.
Release date
https://jira.spring.io/browse/SPR
© 2016 Pivotal Software, Inc. All rights reserved.
http://start.spring.io/#!language=kotlin
© 2016 Pivotal Software, Inc. All rights reserved.
http://start.spring.io/#!language=kotlin
© 2016 Pivotal Software, Inc. All rights reserved.
http://start.spring.io/#!language=kotlin
© 2016 Pivotal Software, Inc. All rights reserved.
http://start.spring.io/#!language=kotlin
© 2016 Pivotal Software, Inc. All rights reserved.
Resources
• https://spring.io/blog/2017/01/04/introducing-kotlin-suppor...
© 2016 Pivotal Software, Inc. All rights reserved.
CfP for JJUG CCC 2017 Spring
•Submit Call for Paper!!! 🙇
•http://www.ja...
Upcoming SlideShare
Loading in …5
×

Spring ❤️ Kotlin #jjug

3,233 views

Published on

JJUG Night Seminar 2017 Feb

Published in: Technology
  • Dating direct: ❶❶❶ http://bit.ly/36cXjBY ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Follow the link, new dating source: ❶❶❶ http://bit.ly/36cXjBY ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • -- DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT -- ......................................................................................................................... ......................................................................................................................... Download FULL PDF EBOOK here { http://bit.ly/2m77EgH } ......................................................................................................................... (Unlimited)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD FULL BOOKS, INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD FULL BOOKS, INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Spring ❤️ Kotlin #jjug

  1. 1. ‹#›© 2016 Pivotal Software, Inc. All rights reserved. ‹#›© 2016 Pivotal Software, Inc. All rights reserved. Spring ❤ Kotlin Toshiaki Maki (@making) tmaki@pivotal.io JJUG Night Seminar 2017 Feb 2017-02-20
  2. 2. © 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
  3. 3. © 2016 Pivotal Software, Inc. All rights reserved. Agenda •Spring Boot with Kotlin •Kotlin support in Spring 5
  4. 4. ‹#›© 2016 Pivotal Software, Inc. All rights reserved. Spring Boot with Kotlin
  5. 5. © 2016 Pivotal Software, Inc. All rights reserved. Spring Initializr
  6. 6. © 2016 Pivotal Software, Inc. All rights reserved. Spring Initializr
  7. 7. © 2016 Pivotal Software, Inc. All rights reserved.
  8. 8. © 2016 Pivotal Software, Inc. All rights reserved.
  9. 9. © 2016 Pivotal Software, Inc. All rights reserved.
  10. 10. © 2016 Pivotal Software, Inc. All rights reserved.
  11. 11. © 2016 Pivotal Software, Inc. All rights reserved.
  12. 12. © 2016 Pivotal Software, Inc. All rights reserved.
  13. 13. © 2016 Pivotal Software, Inc. All rights reserved.
  14. 14. © 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)
 }

  15. 15. © 2016 Pivotal Software, Inc. All rights reserved.
  16. 16. © 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!"
 } 

  17. 17. © 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!"
 } 

  18. 18. © 2016 Pivotal Software, Inc. All rights reserved. // Kotlin open class AppConfig {
 @Bean
 open fun restTemplate() = RestTemplate()
 }
  19. 19. © 2016 Pivotal Software, Inc. All rights reserved. // Kotlin open class AppConfig {
 @Bean
 open fun restTemplate() = RestTemplate()
 } 👇 👇
  20. 20. © 2016 Pivotal Software, Inc. All rights reserved. // Kotlin open class AppConfig {
 @Bean
 open fun restTemplate() = RestTemplate()
 } 👇 👇 😩
  21. 21. © 2016 Pivotal Software, Inc. All rights reserved. Cglib and Spring // Java class AppConfig {
 @Bean
 RestTemplate restTemplate() { return new RestTemplate(); }
 }
  22. 22. © 2016 Pivotal Software, Inc. All rights reserved. Cglib and Spring // Java class AppConfig {
 @Bean
 RestTemplate restTemplate() { return new RestTemplate(); }
 } Singleton
  23. 23. © 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(); }
 }
  24. 24. © 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
  25. 25. © 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>
  26. 26. © 2016 Pivotal Software, Inc. All rights reserved. open class AppConfig {
 @Bean
 open fun restTemplate() = RestTemplate()
 }
  27. 27. © 2016 Pivotal Software, Inc. All rights reserved. open class AppConfig {
 @Bean
 open fun restTemplate() = RestTemplate()
 }
  28. 28. © 2016 Pivotal Software, Inc. All rights reserved. class AppConfig {
 @Bean
 fun restTemplate() = RestTemplate()
 }
  29. 29. © 2016 Pivotal Software, Inc. All rights reserved. class AppConfig {
 @Bean
 fun restTemplate() = RestTemplate()
 } 😍
  30. 30. © 2016 Pivotal Software, Inc. All rights reserved. http://start.spring.io/#!language=kotlin
  31. 31. ‹#›© 2016 Pivotal Software, Inc. All rights reserved. Kotlin support in Spring 5
  32. 32. © 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
  33. 33. © 2016 Pivotal Software, Inc. All rights reserved. Kotlin Support
  34. 34. © 2016 Pivotal Software, Inc. All rights reserved. Kotlin Support
  35. 35. © 2016 Pivotal Software, Inc. All rights reserved. Kotlin Support •Extension Functions •Reified type parameters
  36. 36. © 2016 Pivotal Software, Inc. All rights reserved. Kotlin Support •Extension Functions •Reified type parameters 🤔
  37. 37. © 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); } } }
  38. 38. © 2016 Pivotal Software, Inc. All rights reserved. // Java Foo foo = new Foo(); Bar bar = foo.create(Bar.class);
  39. 39. © 2016 Pivotal Software, Inc. All rights reserved. // Java Foo foo = new Foo(); Bar bar = foo.create(Bar.class); // Kotlin val foo = Foo() val bar = foo.create(Bar::class.java)
  40. 40. © 2016 Pivotal Software, Inc. All rights reserved. // Java Foo foo = new Foo(); Bar bar = foo.create(Bar.class); // Kotlin val foo = Foo() val bar = foo.create(Bar::class.java) 👇
  41. 41. © 2016 Pivotal Software, Inc. All rights reserved. KClass Bar::class // kotlin.reflect.KClass Bar::class.java // java.lang.Class

  42. 42. © 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
  43. 43. © 2016 Pivotal Software, Inc. All rights reserved. // Kotlin val foo = Foo() val bar = foo.create(Bar::class.java)
  44. 44. © 2016 Pivotal Software, Inc. All rights reserved. // Kotlin val foo = Foo() val bar = foo.create(Bar::class.java)
  45. 45. © 2016 Pivotal Software, Inc. All rights reserved. // Kotlin val foo = Foo() val bar = foo.create(Bar::class)
  46. 46. © 2016 Pivotal Software, Inc. All rights reserved. Extension Functions // Kotlin val foo = Foo() val bar = foo.create(Bar::class)
  47. 47. © 2016 Pivotal Software, Inc. All rights reserved. Extension Functions package com.example import kotlin.reflect.KClass fun <T : Any> Foo.create(kclass: KClass<T>) = create(kclass.java)
 FooExtensions.kt
  48. 48. © 2016 Pivotal Software, Inc. All rights reserved. Extension Functions package com.example import kotlin.reflect.KClass fun <T : Any> Foo.create(kclass: KClass<T>) = create(kclass.java)
 👇 FooExtensions.kt
  49. 49. © 2016 Pivotal Software, Inc. All rights reserved. Extension Functions // Kotlin val foo = Foo() val bar = foo.create(Bar::class) 😍
  50. 50. © 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
  51. 51. © 2016 Pivotal Software, Inc. All rights reserved. Reified type parameters inline fun <reified T : Any> Foo.create() = create(T::class.java)

  52. 52. © 2016 Pivotal Software, Inc. All rights reserved. Reified type parameters inline fun <reified T : Any> Foo.create() = create(T::class.java)
👇 👇
  53. 53. © 2016 Pivotal Software, Inc. All rights reserved. val foo = Foo() val bar = foo.create(Bar::class)

  54. 54. © 2016 Pivotal Software, Inc. All rights reserved. val foo = Foo() val bar = foo.create(Bar::class)

  55. 55. © 2016 Pivotal Software, Inc. All rights reserved. Reified type parameters val foo = Foo() val bar = foo.create<Bar>()
 😍
  56. 56. © 2016 Pivotal Software, Inc. All rights reserved. Reified type parameters val foo = Foo() val bar: Bar = foo.create()
 😍
  57. 57. © 2016 Pivotal Software, Inc. All rights reserved. Reified type parameters val foo = Foo() val bar: Bar = foo.create()
 😍 "idiomatic Kotlin code"
  58. 58. © 2016 Pivotal Software, Inc. All rights reserved. "idiomatic Kotlin code" with Spring 5
  59. 59. © 2016 Pivotal Software, Inc. All rights reserved. Application Context // Java ApplicationContext context = ...; Bar bar = context.getBean(Bar.class);
  60. 60. © 2016 Pivotal Software, Inc. All rights reserved. Application Context (Extension) // Kotlin val context = ... val bar = context.getBean(Bar::class)

  61. 61. © 2016 Pivotal Software, Inc. All rights reserved. Application Context (Reified Type) // Kotlin val context = ... val bar = context.getBean<Bar>()

  62. 62. © 2016 Pivotal Software, Inc. All rights reserved. Application Context (Reified Type) // Kotlin val context = ... val bar: Bar = context.getBean()

  63. 63. © 2016 Pivotal Software, Inc. All rights reserved. JdbcTemplate // Java Long count = jdbcTemplate .queryForObject("SELECT count(*) FROM foo" , Long.class);
  64. 64. © 2016 Pivotal Software, Inc. All rights reserved. JdbcTemplate (Extension) // Kotlin val count = jdbcTemplate .queryForObject("SELECT count(*) FROM foo" , Long::class)
  65. 65. © 2016 Pivotal Software, Inc. All rights reserved. JdbcTemplate (Reified Type) // Kotlin val count = jdbcTemplate .queryForObject<Long>( "SELECT count(*) FROM foo")
  66. 66. © 2016 Pivotal Software, Inc. All rights reserved. JdbcTemplate (Reified Type) // Kotlin val count: Long = jdbcTemplate .queryForObject("SELECT count(*) FROM foo")
  67. 67. © 2016 Pivotal Software, Inc. All rights reserved. RestTemplate // Java String foo = restTemplate .getForObject("http://abc.io",String.class);
  68. 68. © 2016 Pivotal Software, Inc. All rights reserved. RestTemplate (Reified Type) // Kotlin val foo = restTemplate .getForObject<String>("http://abc.io");
  69. 69. © 2016 Pivotal Software, Inc. All rights reserved. RestTemplate (Reified Type) // Kotlin val foo: String = restTemplate .getForObject("http://abc.io");
  70. 70. © 2016 Pivotal Software, Inc. All rights reserved. RestTemplate // Java List<Foo> foos = restTemplate .getForObject("http://abc.io", List<Foo>.class);
  71. 71. © 2016 Pivotal Software, Inc. All rights reserved. RestTemplate // Java List<Foo> foos = restTemplate .getForObject("http://abc.io", List<Foo>.class); Compile Error!
  72. 72. © 2016 Pivotal Software, Inc. All rights reserved. RestTemplate // Java List<Foo> foos = restTemplate .exchange("http://abc.io", HttpMethod.GET, null, new ParameterizedTypeReference<List<Foo>>() {}).getBody();
  73. 73. © 2016 Pivotal Software, Inc. All rights reserved. RestTemplate // Java List<Foo> foos = restTemplate .exchange("http://abc.io", HttpMethod.GET, null, new ParameterizedTypeReference<List<Foo>>() {}).getBody(); 💩
  74. 74. © 2016 Pivotal Software, Inc. All rights reserved. RestTemplate (Reified Type)
  75. 75. © 2016 Pivotal Software, Inc. All rights reserved. RestTemplate (Reified Type) // Kotlin val foos: List<Foo> = restTemplate .getForObject("http://abc.io");
  76. 76. © 2016 Pivotal Software, Inc. All rights reserved. RestTemplate (Reified Type) // Kotlin val foos: List<Foo> = restTemplate .getForObject("http://abc.io"); 😍
  77. 77. © 2016 Pivotal Software, Inc. All rights reserved. Spring ❤ Kotlin
  78. 78. © 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
  79. 79. © 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 👇 👇
  80. 80. © 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 👇 👇 🤔
  81. 81. © 2016 Pivotal Software, Inc. All rights reserved.
  82. 82. © 2016 Pivotal Software, Inc. All rights reserved. WebMVC + @Controller in Java @RestController public class UserController { private final UserRepository repo; UserController(UserRepository repo) {/.../} @GetMapping List<User> users() { return repo.findAll(); } }
  83. 83. © 2016 Pivotal Software, Inc. All rights reserved. WebFlux + @Controller in Java @RestController public class UserController { private final ReactiveUserRepository repo; UserController(ReactiveUserRepository repo) {/.../} @GetMapping Flux<User> users() { return repo.findAll(); } }
  84. 84. © 2016 Pivotal Software, Inc. All rights reserved. WebFlux + @Controller in Java @RestController public class UserController { private final ReactiveUserRepository repo; UserController(ReactiveUserRepository repo) {/.../} @GetMapping Flux<User> users() { return repo.findAll(); } } Non-Blocking!!
  85. 85. © 2016 Pivotal Software, Inc. All rights reserved. WebFlux + RouterFunction in Java
  86. 86. © 2016 Pivotal Software, Inc. All rights reserved. WebFlux + RouterFunction in Java RouterFunction<?> routes = route(GET("/users"), req -> ok() .body(repo.findAll(), User.class));
  87. 87. © 2016 Pivotal Software, Inc. All rights reserved. WebFlux + RouterFunction in Kotlin { accept(APPLICATION_JSON).apply { GET("/users",{ ok().body(fromPublisher(repo.findAll()) }) } }
  88. 88. © 2016 Pivotal Software, Inc. All rights reserved. Check source code!! • https://github.com/mix-it/mixit • https://github.com/making/demo-router-functions
  89. 89. © 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?
  90. 90. © 2016 Pivotal Software, Inc. All rights reserved. Release date https://jira.spring.io/browse/SPR
  91. 91. © 2016 Pivotal Software, Inc. All rights reserved. http://start.spring.io/#!language=kotlin
  92. 92. © 2016 Pivotal Software, Inc. All rights reserved. http://start.spring.io/#!language=kotlin
  93. 93. © 2016 Pivotal Software, Inc. All rights reserved. http://start.spring.io/#!language=kotlin
  94. 94. © 2016 Pivotal Software, Inc. All rights reserved. http://start.spring.io/#!language=kotlin
  95. 95. © 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
  96. 96. © 2016 Pivotal Software, Inc. All rights reserved. CfP for JJUG CCC 2017 Spring •Submit Call for Paper!!! 🙇 •http://www.java-users.jp/?p=2830

×