Slide 1

Slide 1 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 1 From zero to hero in 5 minutes with Spring Boot Stéphane Nicoll, Spring Framework Committer, Pivotal @snicoll

Slide 2

Slide 2 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 2 http://github.com/snicoll @snicoll Stéphane Nicoll [email protected]

Slide 3

Slide 3 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 3

Slide 4

Slide 4 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring IO X GRAILS XD BOOT GRAILS Full-stack, Web XD Stream, Taps, Jobs BOOT Bootable, Minimal, Ops-Ready Big, Fast, Flexible Data Web, Integration, Batch WEB Controllers, REST,
 WebSocket INTEGRATION Channels, Adapters,
 Filters, Transformers BATCH Jobs, Steps,
 Readers, Writers BIG DATA Ingestion, Export,
 Orchestration, Hadoop DATA NON-RELATIONAL RELATIONAL CORE GROOVY FRAMEWORK SECURITY REACTOR

Slide 5

Slide 5 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Introduction ! Single point of focus (as opposed to large collection of spring-* projects) ! A tool for getting started very quickly with Spring ! Common non-functional requirements for a "real" application ! Exposes a lot of useful features by default ! Gets out of the way quickly if you want to change defaults ! An opportunity for Spring to be opinionated 4 “ Spring Boot lets you pair-program with the Spring team. Josh Long, @starbuxman

Slide 6

Slide 6 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Let’s get started 5

Slide 7

Slide 7 text

Demo Unless otherwise indicated, these slides are 
 © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Getting started, ! really quick Spring Boot

Slide 8

Slide 8 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Simple Groovy App ! Let’s create a very simple hero.groovy app ! ! ! ! ! ! ! Boot CLI post process such script to add things such as import statements X class HomeController { " ! @RequestMapping("/")" String home() {" 'Hello world!'" }" }

Slide 9

Slide 9 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 7 import org.springframework.boot.SpringApplication" import org.springframework.boot.autoconfigure.EnableAutoConfiguration" import org.springframework.web.bind.annotation.RequestMapping" import org.springframework.web.bind.annotation.RestController" " @Grab("org.springframework.boot:spring-boot-starter-web:1.0.2.RELEASE")" @EnableAutoConfiguration" @RestController" class HomeController {" " @RequestMapping("/")" String home() {" 'Hello world!'" }" " public static void main(String[] args) {" SpringApplication.run(HomeController.class, args);" }" }

Slide 10

Slide 10 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ @EnableAutoConfiguration ! ! ! ! ! ! Attempts to auto-configure your application ! Backs off as you define your own beans ! Regular @Configuration classes ! Usually with @ConditionalOnClass and @ConditionalOnMissingBean 8 @Configuration" @EnableAutoConfiguration" class HomeController {" " public static void main(String[] args) {" SpringApplication.run(MyApplication.class, args);" }" }

Slide 11

Slide 11 text

Demo Unless otherwise indicated, these slides are 
 © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Packaging ! production and cloud-ready Spring Boot

Slide 12

Slide 12 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Packaging for production ! ! ! Externalizing Configuration to Properties • Just put application.properties in your classpath or next to you jar, e.g. ! ! ! • Can be overridden: java -jar hero.jar —server.port=6080 • Can be injected: java -jar hero.jar —conference=Breizhcamp 10 server.port = 9000 @Value("${conference}")" private String conference; $ spring jar hero.jar hero.groovy" $ java -jar hero.jar

Slide 13

Slide 13 text

Demo Unless otherwise indicated, these slides are 
 © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Getting started, ! with Java Spring Boot

Slide 14

Slide 14 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Simple Java app ! Create a skeleton project on http://start.spring.io ! Choose the web option and download the project ! Add a simple controller alongside the app X @RestController" public class HomeController {" " " @RequestMapping("/")" " public String home() {" " " return "Hello World!";" " }" }

Slide 15

Slide 15 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Testing ! Testing with a random port using @IntegrationTest X @RunWith(SpringJUnit4ClassRunner.class)" @SpringApplicationConfiguration(classes = Application.class)" @WebAppConfiguration" @IntegrationTest("server.port=0")" public class HomeControllerIntegrationTest {" " @Value("${local.server.port}")" private int port;" " @Test" public void runAndTestHttpEndpoint() {" String url = "http://localhost:" + this.port + "/";" String body = new RestTemplate().getForObject(url, String.class);" assertEquals("Hello World!", body);" } " }

Slide 16

Slide 16 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Boot Starters ! ! ! ! ! Standard Maven POMs ! Define dependencies that we recommend 12 " org.springframework.boot" spring-boot-starter-web"

Slide 17

Slide 17 text

Demo Unless otherwise indicated, these slides are 
 © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Spring Boot

Slide 18

Slide 18 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Using Spring Data MongoDB ! Add spring-boot-starter-data-mongodb ! Add a simple UserAccount entity with some obvious attributes ! A simple repository interface can be defined as follows: ! ! ! ! ! We can easily add a test for that, too. X @Repository" public interface UserAccountRepository extends MongoRepository {" " UserAccount findByUsername(String username);" " Collection findByFamilyName(String familyName);" }

Slide 19

Slide 19 text

Demo Unless otherwise indicated, these slides are 
 © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Exposing the repository Spring Boot

Slide 20

Slide 20 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Using Spring Data Rest ! Add spring-boot-starter-data-rest ! Update the repository as follows ! ! ! ! ! ! We can change the baseURI by overriding RepositoryRestMvcConfiguration X @RepositoryRestResource(path = "users")" public interface UserAccountRepository extends MongoRepository {" " @RestResource(path = "username")" UserAccount findByUsername(@Param("name") String username);" " List findByFamilyName(@Param("name") String familyName);" }

Slide 21

Slide 21 text

Demo Unless otherwise indicated, these slides are 
 © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Security integration Spring Boot

Slide 22

Slide 22 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Using Spring Security ! Add spring-boot-starter-security ! Create an authentication manager to secure the endpoints with custom users ! ! ! ! ! ! ! Method security can be added as well X @Configuration" static class AuthenticationSecurity extends GlobalAuthenticationConfigurerAdapter {" " @Override" public void init(AuthenticationManagerBuilder auth) throws Exception {" auth.inMemoryAuthentication()" .withUser("hero").password("hero").roles("HERO", "USER").and()" .withUser("user").password("user").roles("USER");" }" }

Slide 23

Slide 23 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Actuator ! Adds common non-functional features to your application • MVC endpoints • JMX support ! Examples • Health check • Environment information • Autoconfig • Metrics • … 16

Slide 24

Slide 24 text

Demo Unless otherwise indicated, these slides are 
 © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Adding common non-functional features Spring Boot

Slide 25

Slide 25 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ If we had more time… 18 Building a war Spring Batch Spring Integration Spring AMQP Spring JMS SpringApplicationBuilder CommandLineRunner Profile YAML support @ConfigurationProperties Logging Static resources Thymeleaf Websocket Redis Disabling auto-config CLI customization Servlet container customization

Slide 26

Slide 26 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 19 Learn More. Stay Connected. ! Start your project at start.spring.io ! Getting started guides ! Follow @springboot ! StackOverflow - #spring-boot Twitter: twitter.com/springcentral YouTube: spring.io/video LinkedIn: spring.io/linkedin Google Plus: spring.io/gplus

Slide 27

Slide 27 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 20