[slides] a short intro to Spring Boot • [live coding] building an app from scratch • — ☕️ break — • Part #2: More on Spring Boot • Boot autoconfig and starters • Externalized configuration
of focus • Getting started 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
features get a lot done for 12factor.net • PaaS friendly: fast startup, devops oriented $ mvn package $ cf push devoxx -p target/devoxx-0.0.1-SNAPSHOT.jar $ cf scale devoxx -i 4
• backs off as you define your own beans • Regular @Configuration classes, @Conditional* annotations @Configuration @ComponentScan @EnableAutoConfiguration public class MyApplication { } @SpringBootApplication public class MyApplication { }
• Create a new hello-service-auto-configuration project • Only one mandatory dependency • Should contain specific dependencies and autoconfiguration classes
implements HelloService { public String sayHello() { System.out.println("Hello Console"); } } • This default implementation will be shipped with the autoconfig project but should not be used if the application provides one.
autoconfig project as a dependency in our main project <dependency> <groupId>org.test.devoxx</groupId> <artifactId>hello-service-auto-configuration</artifactId> <version>...</version> </dependency>
Startup implements CommandLineRunner { @Autowired private HelloService helloService; @Override public void run(String... args) throws Exception { helloService.sayHello(); } } • Our command line app now uses a Hello Service • running this will use the default implementation
{ return () -> LoggerFactory.getLogger(DemoApplication.class) .info("Hello from logs"); } • Add a @Bean definition in our DemoApplication class • We provide our own impl, so the default one won’t be created
environments • properties and YAML formats • Dozens of out-of-the-box configuration keys • Supports inheritance/overriding • Type safe configuration with @ConfigurationProperties • IDE support with configuration metadata
from java:comp/env. 3.Java System properties (System.getProperties()). 4.OS environment variables. 5.application-{profile}.properties or application-{profile}.yml 6.application.properties or application.yml 7.@PropertySource annotations on your @Configuration classes. 8.Default properties (specified using SpringApplication.setDefaultProperties).
Logback with colored output • Default is INFO (use « -- debug » on the command line) • CONSOLE by default (can configure a file output) logging: file: logs/application_log level: ROOT: WARN org.example: INFO org.example.acme: DEBUG
HelloService { @Value("${hello.target:console}") private String target; public String sayHello() { System.out.println("Hello " + this.target); } } • Delete the @Bean HelloService in the application • Update the default one with a property attribute
hello: name: devoxx france java -jar devoxx-0.0.1-SNAPSHOT.jar \ --hello.name=Foo • set that property value with a application.yml key • or a command line argument!
a sensible way (HTML, JSON, etc) • Customize or extend with ErrorAttributes or an « error » view • Create dedicated error pages with EmbeddedServletContainerCustomizer
« Microservices Bootcamp » 13:30-16:30 - Paris 221M with Josh Long and Sébastien Deleuze • Modern Enterprise Java architectures with Spring 4.1 Thursday 13:00-13:50 - Amphi bleu with Juergen Hoeller and Stéphane Nicoll • The Spring BOF Thursday 19:30-20:30 - Neuilly 251