Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless...
Transcript of Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless...
![Page 1: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/1.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/1
Spring Framework 5Themes & Trends
Preparing for 2017+
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Juergen HoellerSpring Framework Lead
Pivotal
![Page 2: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/2.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/2
Spring Framework 4.3
■ Last 4.x feature release!
■ Generally available since June 2016
■ Extended support life until 2019
● on JDK 6, 7, 8
● on Tomcat 6, 7, 8.0, 8.5
● on WebSphere 7, 8.0, 8.5 and 9
■ Programming model refinements brought forward to JDK 6+
● DI & MVC refinements
● composed annotations
![Page 3: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/3.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/3
The State of the Art: Component Classes
@Service
@Lazy
public class MyBookAdminService implements BookAdminService {
// @Autowired
public MyBookAdminService(AccountRepository repo) {
...
}
@Transactional
public BookUpdate updateBook(Addendum addendum) {
...
}
}
![Page 4: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/4.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/4
Configuration Classes with Autowired Constructors
@Configuration
public class MyBookAdminConfig {
private final DataSource bookAdminDataSource;
// @Autowired
public MyBookAdminConfig(DataSource bookAdminDataSource) {
this.bookAdminDataSource = bookAdminDataSource;
}
@Bean
public BookAdminService myBookAdminService() {
MyBookAdminService service = new MyBookAdminService();
service.setDataSource(this.bookAdminDataSource);
return service;
}
}
![Page 5: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/5.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/5
Annotated MVC Controllers
@Controller
@CrossOrigin
public class MyRestController {
@RequestMapping(path="/books/{id}", method=GET)
public Book findBook(@PathVariable long id) {
return this.bookAdminService.findBook(id);
}
@RequestMapping(path="/books/new", method=POST)
public void newBook(@Valid Book book) {
this.bookAdminService.storeBook(book);
}
}
![Page 6: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/6.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/6
Precomposed Annotations for MVC Controllers
@RestController
@CrossOrigin
public class MyRestController {
@GetMapping("/books/{id}")
public Book findBook(@PathVariable long id) {
return this.bookAdminService.findBook(id);
}
@PostMapping("/books/new")
public void newBook(@Valid Book book) {
this.bookAdminService.storeBook(book);
}
}
![Page 7: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/7.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/7
Spring Framework 5
■ A new application framework generation for 2017+
■ 5.0 RC2 out soon; GA expected in July
■ 5.1 to follow in early 2018
■ Major baseline upgrade
● JDK 8+, Servlet 3.1+, JPA 2.1+, JMS 2.0+
● support for JUnit 5 (next to JUnit 4.12)
■ Key infrastructure themes
● JDK 9, Jigsaw, HTTP/2, Servlet 4, Kotlin
● functional style and reactive architectures
![Page 8: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/8.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/8
JDK 9
HTTP/2
Functional
Reactive
![Page 9: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/9.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/9
JDK 9: Not Just Jigsaw
■ Many general JVM improvements
● Compact Strings, G1 by default, TLS protocol stack
■ Jigsaw – module path as structured alternative to class path
● symbolic module names and requires/exports metadata for jar files
■ JDK 9 GA scheduled for September 2017
● Spring 5 is fully aligned in terms of JDK 9's policies and constraints
■ For a smooth immediate upgrade, stay in class path mode!
● Spring 4.3 & 5.0 are generally compatible with JDK 9 out of the box
![Page 10: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/10.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/10
Using Jigsaw with Spring
■ Framework jars as Jigsaw-compliant modules on the module path
● internally declaring module-info for each jar (in consideration for 6.0)
● or as “automatic modules” (for the time being in 4.3 & 5.x)
■ Separate module namespace, following Maven Central jar naming
● spring-context, spring-jdbc, spring-webmvc
■ An application's module-info.java may refer to framework modules
module my.app.db {
requires spring.jdbc;
}
![Page 11: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/11.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/11
JDK 9
HTTP/2
Functional
Reactive
![Page 12: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/12.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/12
The Importance of HTTP/2 (RFC 7540)
■ Enormous benefits over HTTP 1.1 (1997 → 2017)
● binary protocol
● TLS (SSL) everywhere
● connection multiplexing
● headers compression
● request prioritization
● push of correlated resources
■ Browsers already implement HTTP/2 over TLS
● major websites work with HTTP/2 already: Google, Twitter, etc
● We need to embrace it in Java land as well!
![Page 13: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/13.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/13
Spring 5 and HTTP/2
■ Servlet 4.0 specification expected to go final in July 2017
● enforces support for HTTP/2 in Servlet containers
● new PushBuilder API for pushing additional resources to a client
■ Native HTTP/2 support in current Servlet 3.1 containers
● Tomcat 8.5 / 9.0, Jetty 9.3 / 9.4, Undertow 1.3 / 1.4
● special setup for ALPN on JDK 8 (comes out of the box in JDK 9)
■ Let's enable HTTP/2 as soon as possible...
● Spring's focus: native HTTP/2 on top of Tomcat / Jetty / Undertow
● smooth upgrade to Servlet 4.0 containers once available
![Page 14: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/14.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/14
JDK 9
HTTP/2
Functional
Reactive
![Page 15: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/15.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/15
Functional Style vs Annotation Style
■ Spring 4.3 wraps up a well-established annotation story
● a comprehensive annotation-based component model
● with loosely coupled, self-descriptive endpoint classes
■ Spring 5 provides functional-style APIs as an alternative
● programmatic bean registration and endpoint composition
● no need for annotations or scanning, even avoiding reflection
■ First-class support for the Kotlin language out of the box
● Java 8 is a fine foundation for functional-style Java APIs
● Spring's Kotlin extensions make code even more concise
![Page 16: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/16.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/16
Programmatic Bean Registration with Java 8
// Starting point may also be AnnotationConfigApplicationContext
GenericApplicationContext ctx = new GenericApplicationContext();
ctx.registerBean(Foo.class);
ctx.registerBean(Bar.class,
() -> new Bar(ctx.getBean(Foo.class));
// Or alternatively with some bean definition customizing
GenericApplicationContext ctx = new GenericApplicationContext();
ctx.registerBean(Foo.class, Foo::new);
ctx.registerBean(Bar.class,
() -> new Bar(ctx.getBean(Foo.class),
bd -> bd.setLazyInit(true));
![Page 17: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/17.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/17
Programmatic Bean Registration with Kotlin
// Java-style usage of Spring's Kotlin extensions
val ctx = GenericApplicationContext()
ctx.registerBean(Foo::class)
ctx.registerBean { Bar(it.getBean(Foo::class)) }
// Gradle-style usage of Spring's Kotlin extensions
val ctx = GenericApplicationContext {
registerBean<Foo>()
registerBean { Bar(it.getBean<Foo>()) }
}
![Page 18: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/18.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/18
JDK 9
HTTP/2
Functional
Reactive
![Page 19: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/19.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/19
The Importance of Reactive Architectures
![Page 20: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/20.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/20
Reactor 3: Reactive Streams with Backpressure
![Page 21: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/21.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/21
Spring MVC on Servlets ↔ Spring WebFlux on Reactor
![Page 22: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/22.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/22
Reactive Web Controller with Repository Interop
@Controller
public class MyReactiveWebController {
private final UserRepository repository;
public MyReactiveWebController(UserRepository repository) {
this.repository = repository;
}
@GetMapping("/users/{id}")
public Mono<User> getUser(@PathVariable Long id) {
return this.repository.findById(id);
}
@GetMapping("/users")
public Flux<User> getUsers() {
return this.repository.findAll();
}
}
![Page 23: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/23.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/23
Functional Web Endpoints with Method References
RouterFunction<?> router =
route(GET("/users/{id}"), handlerDelegate::getUser)
.andRoute(GET("/users"), handlerDelegate::getUsers);
public class MyReactiveHandlerDelegate {
...
public Mono<ServerResponse> getUser(ServerRequest request) {
Mono<User> user = Mono.justOrEmpty(request.pathVariable("id"))
.map(Long::valueOf).then(this.repository::findById);
return ServerResponse.ok().body(user, User.class);
}
public Mono<ServerResponse> getUsers(ServerRequest request) {
Flux<User> users = this.repository.findAll();
return ServerResponse.ok().body(users, User.class);
}
}
![Page 24: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/24.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/24
Functional Web Endpoints in Lambda Style
UserRepository repository = ...;
RouterFunction<?> router =
route(GET("/users/{id}"),
request -> {
Mono<User> user = Mono.justOrEmpty(request.pathVariable("id"))
.map(Long::valueOf).then(repository::findById);
return ServerResponse.ok().body(user, User.class);
})
.andRoute(GET("/users"),
request -> {
Flux<User> users = repository.findAll();
return ServerResponse.ok().body(users, User.class);
});
![Page 25: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/25.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/25
JDK 9
HTTP/2
Functional
Reactive
![Page 26: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/26.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/26
Spring Framework 5.0RC2 out soon!
JDK 8 baselineearly support for JDK 9 & Servlet 4
functional style: Java 8, Kotlinreactive web endpoint model
![Page 27: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/27.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/27
Consider reactive datastores:Spring Data “Kay”
M4 out soon!
built on Spring Framework 5.0reactive repository model
Mongo, Cassandra, Redis, etc
![Page 28: Spring Framework 5 Themes & Trendsjug.bg/ftp/jPrime/2017/spring5-themes-trends.pdf · Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97af787925279d72b9825/html5/thumbnails/28.jpg)
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/28
Stay tuned for...Spring Boot 2.0!
M2 out soon!
built on Spring Framework 5.0dependency baseline upgrade
reactive web starters