spring boot for web apps

42
Spring Boot for Web Apps Rossen Stoyanchev

Upload: rossen-stoyanchev

Post on 07-Jan-2017

1.660 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Spring Boot for Web Apps

Spring Boot for Web AppsRossen Stoyanchev

Page 2: Spring Boot for Web Apps

Learning Curve for Java Devs● Traditionally very high

● Has been even a kind of source of pride

● What it takes to be a Java enterprise developer !

Page 3: Spring Boot for Web Apps

Convention Over Configuration● We watched what other languages/platforms did

● Shrugged it off

● “It will never work for the apps I build”

Page 4: Spring Boot for Web Apps

Until We Could Ignore No More● RoR, Node

● Productivity grew in importance

● The bar became too high for new developers

Page 5: Spring Boot for Web Apps

Try We Did● Grails did it the Groovy way

● Play Framework did it in Java, sort of

Page 6: Spring Boot for Web Apps

What About Spring?● Productivity at heart from the start

● Reduce boilerplate code

● Intuitive defaults

Page 7: Spring Boot for Web Apps

Much Beyond Spring● App server

● Deployment model

● 3rd party library choices

Page 8: Spring Boot for Web Apps

Previous Attempts● Build a better app server

● Smart code generation (aka Spring Roo)

Page 9: Spring Boot for Web Apps

Rise of Full-Stack Java Frameworks● Focus on application development

● Little or zero choice to start

● Operational aspects

Page 10: Spring Boot for Web Apps

Could We Do The Same With Spring?● https://jira.spring.io/browse/SPR-9888

● Yes We Can

Page 11: Spring Boot for Web Apps

Spring Boot● SPR-9888 / SpringOne 2012

● spring.io live launch at SpringOne 2013 … version 0.5.0.M6

● 1.0.0.RELEASE on April 1, 2014

Page 12: Spring Boot for Web Apps

Spring Boot Adoption● Promising from the start

● Success took everyone by surprise

● Grails built on Spring Boot

Page 13: Spring Boot for Web Apps

Spring Boot Downloads

Page 14: Spring Boot for Web Apps

The Big Whoop● Runnable server app in a tweet

● Opinionated with no strings attached

● Security, metrics, health checks

● No code generation

Page 15: Spring Boot for Web Apps

What MakesTweetable App

Tick?

Page 16: Spring Boot for Web Apps

Start Boot Project● http://start.spring.io

● Alt + Enter

● Save demo.zip

Page 17: Spring Boot for Web Apps

Executable Jar● Single Jar with dependencies

● Run with java -jar

● Right-click in IDE

Page 18: Spring Boot for Web Apps

Executable Jar Creation● Spring Boot Maven/Gradle plugins

● mvn package

● Re-packages as executable jar

Page 19: Spring Boot for Web Apps

About Executable Jars● The way command line Java should be

● No installation, classpath, scripts

● Cloud-friendly

Page 20: Spring Boot for Web Apps

Boot Web Project● http://start.spring.io

● Select “Web” dependency

● Alt + Enter

Page 21: Spring Boot for Web Apps

Executable Web App Jar● Like “war” but executable

● Right-click in IDE … forget web plugins!

● java -jar or mvn spring-boot:run (exploded)

Page 22: Spring Boot for Web Apps

Embedded Server● Tomcat, Jetty, Undertow

● Customize via application.properties

● Programmatically

Page 23: Spring Boot for Web Apps

Make Jar Not War● Jar more popular

● But it’s easy if you need “.war”

war

Page 24: Spring Boot for Web Apps

Starter POMs● spring-boot-starter-*

● Each starter has group of dependencies

● Cohesive versioning

Page 25: Spring Boot for Web Apps

Web Starter Pom● spring-boot-starter-web

● Tomcat / Spring MVC / Jackson / Hibernate-Validator

● No versions to specify

Page 26: Spring Boot for Web Apps

Auto Configuration● Default Spring config

● Opinions based on your classpath

● Intelligently adapts to explicit config

Page 27: Spring Boot for Web Apps

Auto Config Is Not Magic● Spring Java config + various @Conditional annotations

● spring-boot-autoconfigure module

● Auto-config report on startup with --debug

Page 28: Spring Boot for Web Apps

What We Have To Start● Executable jar with embedded server

● Stack of dependencies

● Auto-config

Page 29: Spring Boot for Web Apps

Ready To Code!

Page 30: Spring Boot for Web Apps

BeyondTweetable Apps

Page 31: Spring Boot for Web Apps

Is This About Getting Started?● It gets even better after getting started

● Opinionated but not binding

● Production-ready

Page 32: Spring Boot for Web Apps

Actuator● Add spring-boot-starter-actuator

● Try built-in endpoints

/mappings - request mappings/env - properties/dump - thread dump…

Page 33: Spring Boot for Web Apps

Security● Add spring-boot-starter-security

● Try built-in endpoints again

● Default username “user” + random password (logged)

Page 34: Spring Boot for Web Apps

Externalized Config● application.properties

● Type-safe properties via @ConfigurationProperties

● Each auto-config exposes prefixed properties

Page 35: Spring Boot for Web Apps

Developing with Spring Boot● Find a starter pom

● Read up docs for related auto-config

● See available properties exposed by auto-config

● Browse *AutoConfiguration classes when needed

Page 36: Spring Boot for Web Apps

Working withStatic Resources

Page 37: Spring Boot for Web Apps

Static Resources - html, js, css● Bundled in executable jar

● “/**” served from /static , /public, /resources

● “/webjars/**” served from jar files in Webjars format

Page 38: Spring Boot for Web Apps

Reloading Static Resources● mvn spring-boot:run (exploded)

● IDE hot reloading in debug mode

● Use Spring profiles with “file:” location in “dev”

Page 39: Spring Boot for Web Apps

Reloading With Dev Tools (new in 1.3)

● Auto restart after changes

● Works with LiveReload in browser

● Develop with pleasure

Page 40: Spring Boot for Web Apps

Testing with Spring MVC Test● Server-side integration tests

● Container-less using mock request & response

● Fluent API

● HtmlUnit, WebDriver, Geb integration

Page 41: Spring Boot for Web Apps

@WebIntegrationTest ● Spring Boot test annotation

● Spin up web app including embedded container

● TestRestTemplate to perform requests

Page 42: Spring Boot for Web Apps

Thank YouQ & A