Skip to main content

Command Palette

Search for a command to run...

Getting Started with Spring Boot

Published
•3 min read•View as Markdown

The World Before Spring Boot:

Setting up the Spring project before Spring Boot was not easy!

  1. Dependency Management (pom.xml)

  2. Define Web App Configuration(web.xml)

  3. Manage Spring Beans(context.xml)

  4. 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?

  1. Spring initializer

  2. Spring Boot Starter project

  3. Spring Boot auto-configuration

  4. Spring Boot dev-tools

Production Ready?

  1. Logging

  2. Different Configurations in different environments ( Profiles, ConfigurationProperties)

  3. Monitoring (Spring boot actuator)

Build Quickly

SPRING BOOT STARTER PROJECTS

I need a lot of frameworks to build application features:

  1. Build a Rest API: I need Spring, Spring MVC, Tomcat, and JSON conversion...

  2. 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:

  1. Web Application and Rest API: Spring boot starter web (Spring-webmvc, spring-web, spring-boot-starter-tomcat, spring-boot-starter-json)

  2. Unit Tests: Spring boot starter test

  3. Talk to database using JPA- Spring Boot Starter Data JPA

  4. Talk to database using JDBC: Spring boot starter JDBC

  5. 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:

  1. Development environment

  2. Testing/QA environment

  3. Staging environment

  4. 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:

  1. Install Java

  2. Install Web/Application Server (Tomcat/WebLogic etc)

  3. 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,

  1. Install Java

  2. 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