Why should you use Spring Boot?
• Stability
◦ core and central models in Spring ecosystems don’t change quickly
◦ changes are backwards compatible
• based on JVM
• connectivity
◦ makes it easy to connect with any data access technology
• cloud-native
◦ follows the deployment principles of cloud applications
◦ is ready for the cloud out of the box
◦ build distributed systems in an easy manner
• flexibility
◦ create many kinds of applications
▪ web app
▪ cmd tool
▪ cmd applications
▪ large scale web app
• open-source
Spring vs Spring Boot
• Spring
◦ web app framework based on Java
◦ tools and libraries to create customized web applications
◦ more complex than Spring Boot
◦ unopinionated view
• Spring Boot
◦ module of Spring
◦ used to create Spring apps which can just run/execute
◦ less complex than Spring
◦ opinionated view
What is Spring Boot?
• A Spring module which aims to simplify the use of Spring framework for Java development
• used to create stand-alone Spring-based applications
• aims at Rapid Application Development
• comes with
◦ auto-dependency resolution
◦ embedded HTTP servers
◦ auto-configuration
◦ management endpoints
◦ Spring Boot CLI
• makes a developer’s life way easier to build an app
Advantages
• provides default configuration to load a set of default configuration and for a quick start of the
application
• creates stand-alone applications with a range of features common to a large number of projects
• comes with embedded tomcat, servlet containers
• provides opinionated view to reduce the developer effort and simplify maven configuration
• provides CLI tools to develop and test applications
• comes with Spring Boot starters
• APIs for monitoring and managing applications in dev and prod
• integrates into Spring ecosystem
◦ Spring JDBC
▪ Java Database Connectivity
▪ access to DB directly
▪ allows you to run SQL directly
▪ connect with db by specifying connection parameters in application.properties file
◦ Spring JPA
▪ Java Persistence API
▪ just a specification! There is no implementation
▪ Hibernate is an implementation of JPA
▪ higher level of abstraction than JDBC
▪ hides SQL
▪ usually uses JDBC it under the hood
▪ usually with Hibernate (ORM tool)
▪ Spring Data JPA
• another layer of abstraction over JPA
• built on top of Hibernate
◦ Spring ORM
◦ Spring Data
▪ Spring Data JPA
▪ makes it easy to access databases and other data access technologies
▪ umbrella project
▪ many subprojects specific to a certain database
▪ Spring Data REST
• expose RESTful resources around Spring Data repositories
• generate links to the collection resource
• not recommended for real-world applications
• database exposed directly as REST services
◦ Spring Security
• avoid boilerplate code
Features of Spring Boot
• Spring Boot CLI
◦ software to run and test Spring Boot applications from command prompt
• Spring Initializr
◦ generates a Spring Boot project ready to start quickly
◦ sets up the structure for you
◦ creates a Spring Boot project where based only on the project details
◦ create an app using Initializr
▪ choose Maven project
▪ add required dependencies
▪ generate project
▪ unzip and cooode
• Auto-configuration
◦ attempts to automatically configure your Spring application based on the jar dependencies
that you have added
◦ non-invasive, can be changed any time (DataSource bean)
◦ enables to create Java applications in an easy way
◦ can be partially excluded @EnableAutoConfiguration(excludeName={...})
• Logging and security
◦ ensures Spring Boot apps are properly secured
◦ easy to debug
• Spring Actuator
◦ you can see what is happening inside a running application
◦ brings production-ready features to the application
▪ monitoring application
▪ gathering metrics
▪ understanding traffic
◦ uses HTTP endpoints to enable us to interact with it
How to create a Spring Boot application using Maven?
• Maven
◦ serves two main functions
▪ dependency management
▪ builds
◦ build tool
▪ tool to compile, test, package up your application
▪ provides a project structure
▪ a pom file
• Spring Initializr
• type some stuff into cmd line
Possible sources of external configuration
• application properties (file)
◦ changing embedded Tomcat server port
• command line properties
• profile specific properties (also file)
◦ any @Component, @Configuration and @ConfigurationProperties can be marked with
@Profile
@Profile(“production”)
public class Nanana {
…
}
@Profile
• way to segregate parts of application configuration
• @Component or @Configuration can be marked with @Profile when it’s loaded
• f. ex. different configurations for different development environments
What happens when a Spring Boot application is executed as “Run as Java application”?
• Automatically launches up the tomcat server
Spring Boot starters
• set of convenient dependency management providers
• can be used in the application to enable dependencies
• make development easy and rapid
• bunches of libraries grouped into one dependency
◦ Spring Boot starter
▪ core starter
▪ logging and configuration support
▪ yaml files
◦ Test Starter
▪ Junit
▪ Mockito
▪ etc
◦ Web Starter
◦ JPA Starter
▪ automatic support for
• H2
• Derby
• Hsqldb
◦ etc
Spring Boot dependency management
• used to manage dependencies automatically
• no need to specify versions for dependencies
Thymeleaf
• server-side Java template engine used for web applications
• integrates well with Spring Framework and Java web applications
• if you want to use you have to add a dependency
Spring Boot dev tools
• set of tools that makes developing easier
• module automatically disabled in production
• changing default properties during development (no caching)
• automatic restart
@RequestMapping
• used to map web requests to Spring Controller annotations
@RestContoller
• equals to @Controller + @ResponseBody
◦ @ResponseBody
▪ tells a controller that the object returned is automatically serialized into JSON and
passed into HttpResponse
@SpringBootApplication
• @EnableAutoConfiguration
◦ enable Spring’s auto-configuration mechanism
◦ registers an auto-configuration class
• @ComponentScan
• @Configuration
◦ allows to register extra beans
◦ allows to import additional configuration classes
@Transactional(readOnly = true)
• transaction management
• optimizations in the transaction infrastructure
• omit table locks
• reject write operations that might be accidentally triggered
@SpringBootConfiguration
• class-level annotation
• indicates that a class provides application configuration
• usually this annotation is used via @SpringBootApplication annotation
• allows configuration to be automatically located (unlike @Configuration annotation)
@ConfigurationProperties
• added to a class definition or a @Bean method in a @Configuration class if you want to bind
and validate some external properties (f. ex. from .properties file)
• a bit like @Value, but centralized
YAML file vs properties file
• YAML
◦ hierarchical structure, format
◦ easier to debug
@GetMapping
• composed annotation
• shortcut for @RequestMapping(method = RequestMethod.GET)
Dependencies for building a JPA application with H2 database
• web-starter
• H2
◦ connection to H2: done automatically by Spring Boot configuration
◦ default db: testdb
• data JPA starter
Relaxed binding
• flexible and intuitive configuration properties binding
• different naming conventions can be used to map a property from configuration into a variable
• Spring Boot automatically normalizes these formats and binds them into corresponding Java
fields
• f. ex. ciezkieZycie can be bound to any of the following
◦ ciezkieZycie
◦ ciezkie_zycie
◦ ciezkie-zycie
◦ CiezkieZycie
• doesn’t have to be a 1 to 1 match
MVC
• Controller
◦ receives a request
◦ handles request flow
◦ middle man between Model and View
◦ shouldn’t contain much code
◦ when Controller receives a request, it asks the Model for information based on the request
◦ should never directly interact with the data logic: that’s the Model’s task
◦ after receiving response from Model, Controller needs to interact with the View to render
the data to the user
• Model
◦ responsible for handling all of the data logic of a request
◦ interacts with database
◦ handles all validation
◦ never worries about handling user requests, what to do on failure or success
• View
◦ concerned with how to present the information the Controller sends
◦ template file
◦ sends template with data back to the controller
• Model and View never interact with each other