Spring Boot makes it easier to create Spring-based applications and services. It removes boilerplate configuration and provides opinionated defaults to simplify setup of common Spring and related technologies. Some benefits include embedded servers reducing complexity, autoconfiguration that wires components together, and starter dependencies that add common libraries. Spring Boot helps create production-ready Spring applications with less effort.
Introduction to SpringBoot
Trey Howard
@thoward333
thoward333
https://www.linkedin.com/in/treyhoward
2.
Why Spring?
● DependencyInjection
○ Promotes more maintainable code
○ Promotes testable code
● First class support for all layers of the stack
○ Tomcat, Jetty, Undertow
○ View: JSP, Velocity, Freemarker, JSON, REST/SOAP
○ Repository: Hibernate, JPA, JDBC
○ Database: RDBMS and NoSQL
○ Testing: JUnit, Mockito, Spring Test
3.
Why Spring Boot?
●More choices = More complexity
● Inconsistent dependency versions
● @Autoconfiguration
4.
● Maven orGradle
● Convention over Configuration
○ application.properties (and -profile.properties)
○ src/main/resources: /templates, /static
● Harmonizes library versions (Spring and non-Spring)
○ Version of Spring Boot is the only thing you have to manage
● Opinionated
○ Embedded Tomcat for MVC app, 1-line config change to Jetty, Undertow or none
○ Detects Spring Security on classpath and wires up sensible defaults
■ You don’t want Spring Security? 1-line change to turn off
■ Don’t like the OOTB settings? Extend this class
● spring-boot-starter-actuator
Benefits of Spring Boot
5.
Creating a SpringBoot Project
● Create a seed project using web, IDE, or CLI
○ http://start.spring.io/
○ Download Spring Source STS (Latest 3.8.1)
○ Install CLI (Latest 1.4.0)
■ Creates, runs & more
● Secret sauce: Parent pom.xml
● Include spring-boot-starter-* modules
6.
Spring Boot CLI
Entireproject is single file: hello.groovy
@RestController
class WebApplication {
@RequestMapping("/")
String home() {
"Hello World!"
}
}
spring run hello.groovy
7.
Live Demo -Create New Boot Project
http://start.spring.io/
8.
Demo - RESTAPI
● Basic REST API
● Hard-coded repository class for now
9.
Demo - JDBC
●spring-boot-starter-jdbc
● Uses JdbcTemplate to query database
10.
Demo - JPA
●spring-boot-starter-data-jpa
● Uses CrudRepository to query database
● Notice it’s an interface, not a class
● Method names drive fields that are queried
○ findByEmail(String email) will query db with ‘...where email = ?’
11.
Demo - JPAData REST
● spring-boot-starter-data-jpa & spring-boot-starter-data-rest
● No @Controller
● Because we have included spring-boot-starter-data-rest, Spring creates
REST endpoints for all @Entity classes
12.
Spring Security withSpring Boot
● spring-boot-starter-security
● Add @EnableWebSecurity
Goodies in SpringWeb (Boot 1.4.0)
● spring-devtools: Dual Classpath and Live Reload
● URL Binding annotations:
○ @GetMapping, @PostMapping, etc (@RequestMapping pre-1.4.0)
○ @RestControllerAdvice (@ControllerAdvice + @ResponseBody pre-1.4.0)
○ @SpringBootTest, @WebMvcTest, @AutoConfigureMockMvc, @MockBean, @SpyBean
● HTTP Options, Head are now supported
● RestTemplate setDefaultUriVariables
● @ConfigurationProperties / @EnableConfigurationProperties
○ Strongly typed properties backed by property files
○ Optional metadata for IDE, extra devtools module will auto-generate metadata
15.
Goodies in SpringSecurity (Boot 1.4.0)
● Can access Principal in SpEL expression in Repository layer @Query:
@Query(“select * from foo where owner = ?#{principal.userId}”)
public Foo getFooForCurrentUser() ...
● mvcMatcher - alternative to antMatcher, has suffix vulnerability /foo vs /foo/
● @EnableAuthorizationServer / @EnableResourceServer
16.
Resources
● Spring BootReference Guide:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
○ Installing Spring Boot CLI:
http://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-installing-spring-bo
ot.html#getting-started-installing-the-cli
● Initializr: http://start.spring.io/
● What’s new in Spring Boot 1.4:
https://spring.io/blog/2016/07/28/spring-boot-1-4-released