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/ From Zero to Hero with Spring Boot Stéphane Nicoll Pivotal

Slide 2

Slide 2 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Hello! 2 @snicoll https://github.com/snicoll @snicoll snicoll@pivotal.io

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 4 @snicoll “ Spring Boot lets you pair-program with the Spring team. Josh Long, @starbuxman

Slide 5

Slide 5 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Introduction to Spring Boot ! 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 5 @snicoll

Slide 6

Slide 6 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ಠ_ಠ Spring Boot is NOT ! A prototyping tool ! Only for embedded container apps ! Sub-par Spring experience ! For Spring beginners only 6 @snicoll

Slide 7

Slide 7 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Installation ! Requirements: • JDK6+ • Maven 3.2+ / Gradle 1.12+ ! Tools: • Spring Tool Suite (STS) - IntelliJ IDEA - Netbeans • Spring CLI (from https://start.spring.io or gvm) 7 @snicoll

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Getting Started Really Quickly 9 @snicoll @RestController public class HomeController { @Value("${conference.name:jsug}") private String conference; @RequestMapping("/") public String home() { return "Hello " + conference; } }

Slide 10

Slide 10 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ First integration test 10 @snicoll @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = DemoApplication.class) @WebIntegrationTest(randomPort = true) public class HomeControllerIntegrationTest { @Value("${local.server.port}") private int port; @Test public void runAndInvokeHome() { String url = "http://localhost:" + port + "/"; String body = new RestTemplate() .getForObject(url, String.class); assertThat(body, is("Hello jsug")); } }

Slide 11

Slide 11 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Add Spring Data JPA 11 @snicoll org.springframework.boot spring-boot-starter-data-jpa com.h2database h2 runtime

Slide 12

Slide 12 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Add a Speaker entity 12 @snicoll @Entity public class Speaker { @GeneratedValue @Id private Long id; private String firstName; private String lastName; private String twitter; @Column(columnDefinition = "TEXT") private String bio; public Speaker(String firstName, String lastName, String twitter) { // initialize attributes } // getters and setters }

Slide 13

Slide 13 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Add a Speaker Repository 13 @snicoll public interface SpeakerRepository extends CrudRepository { Speaker findByTwitter(String twitter); Collection findByLastName(String lastName); }

Slide 14

Slide 14 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Test that repository 14 @snicoll @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = DemoApplication.class) public class SpeakerRepositoryTest { @Autowired private SpeakerRepository speakerRepository; @Test public void testFindByTwitter() throws Exception { Speaker stephane = speakerRepository.save( new Speaker("Stephane", "Nicoll", "snicoll")); assertThat(speakerRepository.findByTwitter("snicoll").getId(), is(stephane.getId())); } }

Slide 15

Slide 15 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Add Spring Data REST 15 @snicoll org.springframework.boot spring-boot-starter-data-rest

Slide 16

Slide 16 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ RESTful repository 16 @snicoll public interface SpeakerRepository extends CrudRepository { @RestResource(path = "by-twitter") Speaker findByTwitter(@Param("id") String twitter); Collection findByLastName(@Param("name") String lastName); }

Slide 17

Slide 17 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Add Spring Security 17 @snicoll org.springframework.boot spring-boot-starter-security

Slide 18

Slide 18 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Security config 18 @snicoll @Configuration public class SecurityConfig 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 19

Slide 19 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Fix our test 19 @snicoll @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = DemoApplication.class) @WebIntegrationTest(randomPort = true) public class HomeControllerIntegrationTest { @Value("${local.server.port}") private int port; @Test public void runAndInvokeHome() { String url = "http://localhost:" + port + "/"; String body = new TestRestTemplate("user", "user") .getForObject(url, String.class); assertThat(body, is("Hello jsug")); } }

Slide 20

Slide 20 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Add Spring Boot actuator 20 @snicoll org.springframework.boot spring-boot-starter-actuator

Slide 21

Slide 21 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Add a health indicator 21 @snicoll @Bean public HealthIndicator jsugHealthIndicator() { return () -> { if (new Random().nextBoolean()) { return Health.up().build(); } else { return Health.down().withDetail("Boooo",42).build(); } }; }

Slide 22

Slide 22 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Add remote shell 22 @snicoll org.springframework.boot spring-boot-remote-shell

Slide 23

Slide 23 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Add a speaker controller 23 @snicoll @Controller public class SpeakerController { private final SpeakerRepository speakerRepository; @Autowired public SpeakerController(SpeakerRepository speakerRepository) { this.speakerRepository = speakerRepository; } @RequestMapping("/ui/speakers/{id}") public String show(@PathVariable Long id, ModelMap model) { Speaker speaker = speakerRepository.findOne(id); if (speaker == null) { throw new SpeakerNotFoundException(); } model.put("speaker", speaker); return "speakers/show"; } @ResponseStatus(HttpStatus.NOT_FOUND) public static class SpeakerNotFoundException extends RuntimeException { } }

Slide 24

Slide 24 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Create speaker show view 24 @snicoll View speaker

Stephane Nicoll

Sample Biography.

Slide 25

Slide 25 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Best experience with PaaS ! Spring Boot features get a lot done for 12factor.net ! PaaS friendly: fast startup, devops oriented 25 @snicoll $ mvn package $ cf push jsug -p target/jsug-0.0.1-SNAPSHOT.jar $ cf scale jsug -i 4

Slide 26

Slide 26 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Starter POMs ! standard POM / gradle files: define dependencies ! many available: batch, integration, web, ampq… ! starter data-jpa = spring-data-jpa + hibernate 26 @snicoll org.springframework.boot spring-boot-starter-web compile("org.springframework.boot:spring-boot-starter-web")

Slide 27

Slide 27 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Writing your own starter ! Add support for X in Boot with a PR! ! Distribute a client lib in your company ! Standardize usage of component X in a platform ! [your use case here] 27 @snicoll

Slide 28

Slide 28 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Let’s write our own starter! 28 @snicoll

Slide 29

Slide 29 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ New auto-config project ! Create a new hello-service-auto-configuration project ! Only one mandatory dependency ! Should contain specific dependencies and auto-configuration classes 29 @snicoll org.springframework.boot spring-boot-autoconfigure

Slide 30

Slide 30 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Add custom service interface ! This is the part that we’re trying to auto-configure ! In a typical use case, this interface comes from a 3rd party lib 30 @snicoll public interface HelloService { String sayHello(); }

Slide 31

Slide 31 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Create a default implementation ! This default implementation will be shipped with the auto-config project but should not be used if the application provides one. 31 @snicoll public class ConsoleHelloService implements HelloService { public String sayHello() { System.out.println("Hello Console"); } }

Slide 32

Slide 32 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Auto-configuration example 32 @snicoll @Configuration @ConditionalOnClass(HelloService.class) public class HelloServiceAutoConfiguration { @ConditionalOnMissingBean @Bean public HelloService helloService() { return new ConsoleHelloService(); } }

Slide 33

Slide 33 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Declare the auto-configuration 33 @snicoll org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ demo.hello.HelloServiceAutoConfiguration // one can order AutoConfigurations // with those annotations @AutoConfigureBefore @AutoConfigureAfter

Slide 34

Slide 34 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Add a dependency to our auto-configuration ! Add our auto-config project as a dependency in our main project 34 @snicoll org.test.jsug hello-service-auto-configuration ...

Slide 35

Slide 35 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Invoke our service when the application starts ! A Hello Service implementation is used on startup ! Running this will use the default implementation 35 @snicoll @Component public class Startup implements CommandLineRunner { @Autowired private HelloService helloService; @Override public void run(String... args) throws Exception { helloService.sayHello(); } }

Slide 36

Slide 36 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Override default (auto-configured) implementation ! Add a @Bean definition in our DemoApplication class ! We provide our own implementation, so the default one won’t be created 36 @snicoll @Bean public HelloService helloService() {
 return () -> LoggerFactory.getLogger(DemoApplication.class) .info("Hello from logs");
 } }

Slide 37

Slide 37 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Add type-safe properties 37 @snicoll @ConfigurationProperties("hello") public class HelloProperties { private String prefix = "Hello "; private String target; // getters and setters }

Slide 38

Slide 38 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Enable properties support 38 @snicoll @EnableConfigurationProperties(HelloProperties.class) @Configuration @ConditionalOnClass(HelloService.class) public class HelloServiceAutoConfiguration {

Slide 39

Slide 39 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Use type-safe properties 39 @snicoll public class ConsoleHelloService implements HelloService { @Autowired private HelloProperties properties; @Override public void run(String... strings) throws Exception { System.out.println(properties.getPrefix() + " " + properties.getTarget()); } }

Slide 40

Slide 40 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Generate properties meta-data ! annotation processor that generates meta-data ! uses Javadoc to build keys descriptions ! default values detected ! manual declaration allowed 40 @snicoll org.springframework.boot spring-boot-configuration-processor true

Slide 41

Slide 41 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Document properties 41 @snicoll @ConfigurationProperties("hello") public class HelloProperties { /** * Prefix of welcome message. */ private String prefix = "Hello "; /** * Target of welcome message. */ private String target; // getters and setters }

Slide 42

Slide 42 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ https://spring.io powered by 42 @snicoll github.com/spring-io/sagan

Slide 43

Slide 43 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Links ! https://projects.spring.io/spring-boot/ ! https://github.com/spring-projects/spring-boot ! https://spring.io/guides ! https://spring.io/blog ! https://spring.io/questions ! https://www.youtube.com/user/SpringSourceDev 43 @snicoll @springboot @springcentral

Slide 44

Slide 44 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 44 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 @snicoll