Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From Zero to Hero in 5 minutes with Spring Boot

From Zero to Hero in 5 minutes with Spring Boot

Avatar for Stéphane Nicoll

Stéphane Nicoll

May 23, 2014
Tweet

More Decks by Stéphane Nicoll

Other Decks in Programming

Transcript

  1. 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
  2. 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]
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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!'" }" }
  9. 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);" }" }
  10. 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);" }" }
  11. 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
  12. 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
  13. 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
  14. 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!";" " }" }
  15. 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);" } " }
  16. 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 <dependency>" <groupId>org.springframework.boot</groupId>" <artifactId>spring-boot-starter-web</artifactId>" </dependency>
  17. 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
  18. 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, Long> {" " UserAccount findByUsername(String username);" " Collection<UserAccount> findByFamilyName(String familyName);" }
  19. 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
  20. 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<UserAccount, Long> {" " @RestResource(path = "username")" UserAccount findByUsername(@Param("name") String username);" " List<UserAccount> findByFamilyName(@Param("name") String familyName);" }
  21. 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
  22. 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");" }" }
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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