#jjug_ccc #ccc_gh5 What's new in Spring Framework 4.3 / Boot 1.4 + Pivotal's Cloud Native Approach
1. What's new in
Spring Framework 4.3/Boot 1.4
+
Pivotal's Cloud Native Approach
2016/05/21 JJUG CCC 2016 Spring
Toshiaki Maki (@making)
Sr. Solutions Architect @Pivotal
#jjug_̲ccc #ccc_̲gh5
2. Who am I ?
•Toshiaki Maki (@making)
•https://blog.ik.am
•Sr. Solutions Architect
•Spring Framework enthusiast
Spring
Framework
徹底⼊入⾨門
(Coming
Soon?)
パーフェクト
Java EE
(Coming
Soon?)
3. Agenda
•Spring Boot 1.4 / Spring Framework 4.3
•Spring Framework 5.0
•Spring Cloud
•Go to Cloud Native
13. Spring Boot with Banner
src/main/resrouces/banner.txt
•1.1 supported Custom Text Banner
14. Spring Boot with Banner
•1.3 supported ANSI Color Banner
${AnsiColor.BRIGHT_GREEN}My Application
${AnsiColor.BRIGHT_YELLOW}Hello!!${AnsiColor.DEFAULT}
src/main/resrouces/banner.txt
29. Testing application slices
•Testing the JPA slice
•Testing the Spring MVC slice
•Testing the JSON slice
for fast tests
(without Embedded Server)
30. Testing the JPA slice
@RunWith(SpringRunner.class)
@DataJpaTest
public class UserRepositoryTests {
@Autowired TestEntityManager em;
@Autowired UserRepository repository;
@Test
public void test() {
em.persist(new User("maki", 20));
User user = this.repository.findByUsername("maki");
assertThat(user.getUsername()).isEqualTo("maki");
assertThat(user.getAge()).isEqualTo(20);
}
}
Test data
creation
31. Testing the Spring MVC slice
@RunWith(SpringRunner.class)
@WebMvcTest(FooController.class)
public class FooControllerTests {
@Autowired MockMvc mvc;
@MockBean FooService fooService;
@Test public void test() {
given(fooService.getFoo("xyz")).willReturn("bar");
mvc.perform(get("/foo")
.andExpect(status().isOk())
.andExpect(content().string("bar"));
}
}
32. Testing the Spring MVC slice
@RunWith(SpringRunner.class)
@WebMvcTest(FooController.class)
public class FooControllerTests {
@Autowired WebClient webClient; // using HtmlUnit
@MockBean FooService fooService;
@Test public void test() {
given(fooService.getFoo("xyz")).willReturn("bar");
HtmlPage page = webClient.getPage("/foo");
HtmlForm form = page.getHtmlElementById("fooForm");
// ...
}
}
33. Testing the Spring MVC slice
@RunWith(SpringRunner.class)
@WebMvcTest(FooController.class)
public class FooControllerTests {
@Autowired WebDriver webDriver; // using Selenium
@MockBean FooService fooService;
@Test public void test() {
given(fooService.getFoo("xyz")).willReturn("bar");
// ...
}
}
34. Testing the JSON slice
@RunWith(SpringRunner.class)
@JsonTest
public class MyJsonTests {
JacksonTester<VehicleDetails> json;
@Test public void testSerialize() {
VehicleDetails details =
new VehicleDetails("Honda", "Civic");
assertThat(json.write(details))
.isEqualToJson("expected.json");
assertThat(json.write(details))
.extractingJsonPathStringValue("@.make")
.isEqualTo("Honda");
}
}
35. Testing the JSON slice
@RunWith(SpringRunner.class)
@JsonTest
public class MyJsonTests {
JacksonTester<VehicleDetails> json;
@Test public void testDeserialize() {
String json =
"{¥"make¥":¥"Ford¥",¥"model¥":¥"Focus¥"}";
assertThat(json.parse(json))
.isEqualTo(new VehicleDetails("Ford", "Focus"));
assertThat(json.parseObject(json).getMake())
.isEqualTo("Ford");
}
}
36. Spring Boot 1.4
•Banner Update
•Test Improvements
•Spring Framework 4.3 Support
•Misc
https://github.com/spring-‐‑‒projects/spring-‐‑‒boot/wiki/Spring-‐‑‒Boot-‐‑‒1.4-‐‑‒Release-‐‑‒Notes
37. Spring Framework 4.3
• Last 4.x feature release!
• 4.3 RC1: April 6th
• 4.3 GA: June 1st, 2016
• DI & MVC refinements
• Composed annotations
• Extended support life until 2020
• on JDK 6, 7, 8 (and JDK 9 on a best-‐‑‒effort basis)
• on Tomcat 6, 7, 8.0, 8.5 (and on best-‐‑‒effort 9.0)
• on WebSphere 7, 8.0, 8.5 and 9 (Classic + Liberty)
from Keynote @ Spring IO 2016
38. Spring Framework 4.3
• Implicit constructor injection
• InjectionPoint like CDI
• ....
• Composed annotations for @RequestMapping
• Composed annotations for web @Scopes
• @SessionAttribute/@RequestAttribute ...
http://docs.spring.io/spring/docs/4.3.0.RC2/spring-‐‑‒framework-‐‑‒reference/htmlsingle/#new-‐‑‒in-‐‑‒4.3
Core
Web
39. Implicit constructor injection
@RestController
public class FooController {
private final FooService fooService;
@Autowired
public FooController(FooService fooService) {
this.fooService = fooService;
}
}
~∼ Spring 4.2
👈
50. @Scope("request", proxyMode=TARGET_CLASS)
public class RequestScopeBean {}
@Scope("session", proxyMode=TARGET_CLASS)
public class SessionScopeBean {}
@Scope("application", proxyMode=TARGET_CLASS)
public class ApplicationScopeBean {}
~∼Spring 4.2
Composed annotations for
web @Scope s
51. @RequestScope
public class RequestScopeBean {}
@SessionScope
public class SessionScopeBean {}
@ApplicationScope
public class ApplicationScopeBean {}
Spring 4.3
Composed annotations for
web @Scope s
52. @GetMapping("foo")
String foo(@SessionAttribute("foo")String foo) {
// equals to sessiong.getAttribute("foo")
}
@GetMapping("bar")
String bar(@RequestAttribute("bar")String bar) {
// equals to request.getAttribute("bar")
}
Spring 4.3
@SessionAttribute/@RequestAttribute
for access to session/request attributes
53. Spring Boot 1.4
•Banner Update
•Test Improvements
•Spring Framework 4.3 Support
•Misc
https://github.com/spring-‐‑‒projects/spring-‐‑‒boot/wiki/Spring-‐‑‒Boot-‐‑‒1.4-‐‑‒Release-‐‑‒Notes
54. Miscellaneous
• Spring Boot 1.4
• Startup error improvements
• Couchbase 2.0 / Neo4J Support
• @JsonComponent
• Spring Security 4.1
• Spring Data Hopper and so on...
• Spring Framework 4.3
• Java Config supports constructor injection
• Programmatic resolution of dependencies
• Cache abstraction refinements
• Built-‐‑‒in support for HTTP HEAD and OPTIONS
• Caffeine Support
• OkHttp3 Support and so on...
56. Spring 5.0
•A new framework generation for 2017+
•5.0 M1 July 2016
•5.0 RC1 December 2016
from Keynote @ Spring IO 2016
57. Spring 5.0
•Major baseline upgrade
•JDK 8+, Servlet 3.0+, JMS 2.0+,
JPA 2.1+, JUnit 5
•JDK 9, Jigsaw
•HTTP/2
•Reactive Architecture
from Keynote @ Spring IO 2016
58. Reactor
•Yet Another Rx library on the JVM
•Natively built on top of Reactive Streams
•Developed by Pivotal
•Non-‐‑‒blocking
•Reactor Core provides lite Rx API
•Flux for 0..N elements
•Mono for 0..1 element
https://projectreactor.io/
60. Spring Reactive
• Embeds Reactor
• Experimental project on Spring 5 reactive support
• Runs on
• Reactor Net
• RxNetty
• Undertow
• Servlet 3.1 containers (Servlet is optional !!)
• Same programming model as Spring MVC
• Will be merged to 5.x branch after 4.3 released
https://github.com/spring-‐‑‒projects/spring-‐‑‒reactive
61. Spring Reactive
@RestController
public class TodosController {
@GetMapping("todos")
Flux<Todo> list() {
return this.repository.list();
}
@PostMapping("todos")
Mono<Void> create(@RequestBody Flux<Todo> stream) {
return this.repository.insert(stream);
}
} https://github.com/sdeleuze/spring-‐‑‒reactive-‐‑‒playground
70. Cloud Native?
•"Software designed to run and scale
reliably and predictably on top of
potentially unreliable cloud-‐‑‒based
infrastructure"
(Duncan C.E. Winn, Free O'Reilly Book: Intro to the Cloud Native Platform)
•Microservices is a part of Cloud Native
72. Continuous
Delivery
Release once every 6
months
More Bugs in production
Release early and often
Higher Quality of Code
DevOps
Not my problem
Separate tools, varied incentives,
opaque process
Shared responsibility
Common incentives, tools, process
and culture
Microservices
Tightly coupled components
Slow deployment cycles waiting
on integrated tests teams
Loosely coupled components
Automated deploy without waiting
on individual components
73. Continuous
Delivery
Release once every 6
months
More Bugs in production
Release early and often
Higher Quality of Code
DevOps
Not my problem
Separate tools, varied incentives,
opaque process
Shared responsibility
Common incentives, tools, process
and culture
Microservices
Tightly coupled components
Slow deployment cycles waiting
on integrated tests teams
Loosely coupled components
Automated deploy without waiting
on individual components
ArchitectureProcessCulture
82. I. Codebase
One codebase
tracked in SCM, many
deploys
II. Dependencies
Explicitly declare and
isolate dependencies
III. Configuration
Store config in the
environment
VI. Processes
Execute app as stateless
processes
V. Build, Release,
Run
Strictly separate build
and run stages
IV. Backing Services
Treat backing
services as attached
resources
IX. Disposability
Maximize robustness
with fast startup and
graceful shutdown
VIII. Concurrency
Scale out via the
process model
VII. Port binding
Export services via port
binding
XII. Admin processes
Run admin / mgmt
tasks as one-‐‑‒off
processes
X. Dev/prod parity
Keep dev, staging, prod
as similar as possible
XI. Logs
Treat logs as event
streams
12 Factors App