Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ From Zero to Hero with Spring Boot Stéphane Nicoll Pivotal
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
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
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
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
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
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
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(); }
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"); } }
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
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
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(); } }
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"); } }