Sub topic 1
What Spring Boot Is and How to Create a Spring Boot Project
Prerequisites
This chapter assumes basic Java classes, user-level Maven or Gradle knowledge, and a basic understanding of HTTP and web applications.
Spring Framework and dependency injection
The Spring Framework helps Java applications create and connect objects. An object managed by Spring is called a bean. Spring keeps these beans in an application context, which is a container that creates and stores them.
Dependency injection is the practice of giving an object the other objects it needs instead of making it construct them itself. For example, a controller can receive a service through its constructor. This reduces coupling and makes components easier to test.
Spring is powerful, but a plain Spring project can require substantial configuration. Developers must choose libraries, configure beans, and set up a web server.
What Spring Boot adds
Spring Boot is an opinionated layer on top of Spring. It chooses common defaults and reduces setup work. Its main features are:
- Starters, which group compatible dependencies.
- Auto-configuration, which creates common beans based on the libraries on the classpath.
- An embedded web server, which lets the application run as a standalone process.
Starters: curated dependency sets
A starter is a dependency bundle for a common application type. For example, a web starter brings in Spring MVC, JSON support, and a compatible embedded server. A test starter brings in common testing libraries.
This approach avoids selecting every transitive dependency manually. The build tool still downloads individual libraries, but the starter provides a tested combination.
Auto-configuration: how Boot configures beans for you
At startup, Spring Boot examines the classpath, configuration properties, and existing beans. It then applies conditional configuration. For example, when web libraries are present and no server bean is supplied, Boot configures a web application and a server.
Auto-configuration supplies defaults, not permanent restrictions. You can replace a default by defining your own bean or changing a property.
The embedded web server
A Boot web application commonly includes an embedded server such as Tomcat. The server runs inside the application process, usually on port 8080. You can start the application with one command and deploy the resulting executable JAR without installing a separate server.
Exercise
Why can a Spring Boot web application run with java -jar?
Show answer
Its dependencies and an embedded web server are packaged with the application, so a separate application-server installation is usually unnecessary.