Bootiful Applications With Spring Boot
Bootiful Applications With Spring Boot
[dsyer,snicoll]@pivotal.io
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Spring IO platform
XD
BOOT
XD
INTEGRATION
DATA
GRAILS
BOOT
Big,
Fast,
Flexible
Data
Channels, Adapters,
Filters, Transformers
Full-stack, Web
BATCH
BIG DATA
WEB
Jobs, Steps,
Readers, Writers
Ingestion, Export,
Orchestration, Hadoop
Controllers, REST,
WebSocket
RELATIONAL
CORE
FRAMEWORK
3
GRAILS
Web,
Integration,
Batch
SECURITY
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
NON-RELATIONAL
GROOVY
REACTOR
@david_syer | @snicoll
github.com/spring-io/sagan
4
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
!
!
!
!
A prototyping tool
Only for embedded container apps
Sub-par Spring experience
For Spring beginners only
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Installation
! Requirements:
Java (>=1.6) + (for Java projects)
Maven 3.2+ or Gradle 1.12+
! Spring Tool Suite has some nice features for Java projects
! Download: https://start.spring.io/spring.zip
! Unzip the distro (approx. 10MB), and find bin/ directory
$ spring help
...
! (You can also install Spring Boot CLI with gvm, brew or MacPorts)
8
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
import org.springframework.web.bind.annotation.RestController
// other imports ...
@RestController
class Example {
@RequestMapping("/")
public String hello() {
return "Hello World!";
}
}
10
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
11
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
12
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
@RequestMapping("/")
public String home() {
return "Hello World!";
}
}
14
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Starter POMs
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
SpringApplication
SpringApplication app = new SpringApplication(MyApplication.class);
app.setShowBanner(false);
app.run(args);
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
SpringApplicationBuilder
! Flexible builder style with fluent API for building SpringApplication with
more complex requirements.
new SpringApplicationBuilder(ParentConfiguration.class)
.profiles("adminServer", "single")
.child(AdminServerApplication.class)
.run(args);
17
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
@EnableAutoConfiguration
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class MyApplication {
}
@SpringBootApplication
public class MyApplication {
}
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
19
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
! Gradle plugin:
apply plugin: 'spring-boot'
$ gradle build
20
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
21
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
22
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
23
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
24
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Using YAML
! Just include snake-yaml.jar
Already available if youre using the starters
! Write an application.yml file
name: BootDragon
server:
port: 9000
26
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
! application.properties
mine.location=classpath:mine.xml
mine.skip=false
27
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Configuration Meta-data
! Annotation processor
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
30
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Spring Profiles
! Activate external configuration with a Spring profile
file name convention e.g. application-development.properties
or nested documents in YAML: server:
address: 192.168.1.100
--spring:
profiles: development
server:
address: 127.0.0.1
--spring:
profiles: production
server:
address: 192.168.1.120
31
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Spring Profiles
! Set the default spring profile(s) in external configuration
spring.profiles.active=default, postgresql
! Add some profile(s) to the active profiles rather than replacing them
spring.profiles.include=another
32
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
33
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Logging
! Spring Boot provides default configuration files for 4 logging frameworks:
Logback, Log4j, Log4j2 and java.util.Logging
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Error handling
! /error handles all errors in a sensible way
Registered as global error page in the servlet container
Add a view that resolve to error to customize the representation
! Default representation
Whitelabel error page for browser if none is found
Standardized JSON format for machine clients
! Customize or extend ErrorAttributes
! Create dedicated error pages via EmbeddedServletContainerCustomizer
37
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
38
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
The Actuator
! Adds common non-functional features to your application and exposes
endpoints to interact with them (REST, JMX)
/info
/health
Audit
If embedded in a web app or web service can use the same port or a different one
(management.port) and/or a different network interface (management.address)
and/or context path (management.context-path).
41
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-remote-shell</artifactId>
</dependency>
42
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Building a WAR
We like launchable JARs, but you can still use WAR format if you prefer. Spring
Boot Tools take care of repackaging a WAR to make it executable. If you want a
WAR to be deployable (in a "normal" container), then you need to use
SpringBootServletInitializer instead of or as well as SpringApplication.
public class ServletInitializer
extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
}
43
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
44
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Customizing @EnableAutoConfiguration
! Disable specific feature
@EnableAutoConfiguration(exclude={WebMvcAutoConfiguration.class})
@SpringBootApplication(exclude={WebMvcAutoConfiguration.class})
45
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
46
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll
Links
! Documentation: http://projects.spring.io/spring-boot
! Source: https://github.com/spring-projects/spring-boot
! Blog: http://spring.io/blog
! Twitter: @SpringBoot, @david_syer, @snicoll
! Email: [email protected], [email protected]
47
Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a
@david_syer | @snicoll