Getting Started with Spring Boot
The World Before Spring Boot:
Setting up the Spring project before Spring Boot was not easy!
Dependency Management (pom.xml)
Define Web App Configuration(web.xml)
Manage Spring Beans(context.xml)
Implement Non-functional requirements (NFRs)
It typically takes a few days to set up for each project and countless hours to maintain.
How does Spring Boot help us?
It helps us production-ready applications build quickly.
Build quickly?
Spring initializer
Spring Boot Starter project
Spring Boot auto-configuration
Spring Boot dev-tools
Production Ready?
Logging
Different Configurations in different environments ( Profiles, ConfigurationProperties)
Monitoring (Spring boot actuator)
Build Quickly
SPRING BOOT STARTER PROJECTS
I need a lot of frameworks to build application features:
Build a Rest API: I need Spring, Spring MVC, Tomcat, and JSON conversion...
Write Unit Tests: I need Spring Test, JUnit, Mockito, ..
How can I group them and make it easy to build applications?
Starters: Convenient dependency descriptors for diff. features
Spring Boot provides various starter projects:
Web Application and Rest API: Spring boot starter web (Spring-webmvc, spring-web, spring-boot-starter-tomcat, spring-boot-starter-json)
Unit Tests: Spring boot starter test
Talk to database using JPA- Spring Boot Starter Data JPA
Talk to database using JDBC: Spring boot starter JDBC
Secure your web application or REST API: Spring Boot Starter Security.
Spring Boot Auto Configuration
I need a lot of configuration to build the Spring app:
ComponentScan, DispatcherServlet, DataSources, JSON Conversion, ..
How to Simplify this?
Auto Configuration: Automated Configuration for your app.
Decided based on:
1. Which frameworks are in the classpath?
2. What is the existing configuration (Annotations etc)?
Example: Spring Boot Starter Web
Dispatcher Servlet (DispatcherServletAutoConfiguration)
Embedded Servlet Container- Tomcat is the default (EmbeddedWebServerFactoryCustomizerAutoConfiguration)
Default Error Pages (ErrorMvcAutoConfiguration)
Bean <-> JSON (JacksonHttpMessageConvertersConfiguration)
Spring Boot DevTools
Increase developer productivity
Why do you need to restart the server manually for every code change?
By use of Spring Boot DevTools, the server restarts automatically on code changes.
Remember: For pom.xml dependency changes, you will need to restart the server.
Production Ready Application
Logging
logging.level.org.springframework = trace
Different logging levels:
trace
debug
info
warning
error
off
In each level, the level below it is also printed. For eg. in the case of logging level trace everything is printed, in the case of logging level debug- info, warning, and error are also printed, similarly in the case of logging level error only errors are printed.
Profiles
Profiles enable us to have different configurations for different environments.
Different environments are:
Development environment
Testing/QA environment
Staging environment
Production environment
Manage Application Configuration
Build a Configuration class.
In Spring Boot, Combination of Configuration properties and profiles is very powerful. It enables you to externalize all configurations you need in your application.
Simplify Deployment with Spring Boot Embedded Servers
Simplify Deployment with Spring Boot Embedded Servers
Traditional approach:
Install Java
Install Web/Application Server (Tomcat/WebLogic etc)
Deploy the application WAR (Web ARchive)
But it is Complex to set up!!
Embedded Server- A simpler alternative
When we use an embedded server, the server i.e. for example tomcat is already a part of the JAR file.
So,
Install Java
Run the JAR file.
So Embedded server helps us simplify the deployment process.
Monitor Application using Spring Boot Actuator
Monitor and manage your application in your production.
Provides several endpoints:
beans: Complete list of Spring beans in your app
health: Application health information
metrics: application metrics
Mapping: Details around request mapping