Skip to content

Expert · Spring Boot basics · topic 52 of 60 · 25 min

Creating a Spring Boot app

Explanation

Spring Boot makes Spring easy to start: generate a project at start.spring.io, add starters such as spring-boot-starter-web, and run it. Auto-configuration sets up sensible defaults, an embedded Tomcat server listens on port 8080, and settings live in application.properties or application.yml, with profiles for dev and prod. Spring Boot 3 and later need Java 17 or newer.

Why Spring Boot?

Plain Spring is powerful but used to need a lot of configuration. Spring Boot adds opinions and defaults: pick what you need with starters, and Boot configures it automatically (a web server, JSON, database connections, logging). You get a runnable app in minutes, packaged as one JAR with the server inside.

Step 1: generate the project

  1. Open start.spring.io (Spring Initializr).
  2. Choose Maven or Gradle, Java, a current Spring Boot version, and Java 21 (or the LTS you use).
  3. Group com.shop, artifact catalog.
  4. Add dependencies: Spring Web, Validation, Spring Data JPA, and a database driver (PostgreSQL, or H2 for practice). Optionally Spring Boot Actuator.
  5. Generate, unzip, and open the folder in your IDE.
java
package com.shop.catalog;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication          // = @Configuration + @EnableAutoConfiguration + @ComponentScan
public class CatalogApplication {
    public static void main(String[] args) {
        SpringApplication.run(CatalogApplication.class, args);
    }
}
Component scanning starts from this class's package, so keep your classes in com.shop.catalog or below.
bash
./mvnw spring-boot:run          # or run main() from the IDE
# … Tomcat started on port 8080 (http)
# … Started CatalogApplication in 1.8 seconds

Starters and auto-configuration

A starter is one dependency that brings a tested set of libraries: spring-boot-starter-web brings Spring MVC, an embedded Tomcat and Jackson for JSON; spring-boot-starter-data-jpa brings Hibernate and Spring Data. Auto-configuration then looks at what is on the classpath and in your settings and creates the matching beans (a DataSource if a driver and URL are present, for example). You can always override a default by defining your own bean or property.

Configuration: properties and profiles

ini
# src/main/resources/application.properties
server.port=8080
spring.application.name=catalog
spring.datasource.url=jdbc:postgresql://localhost:5432/catalog
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASSWORD}
app.gst-rate=18
Values like ${DB_PASSWORD} are read from environment variables, so secrets stay out of the code.

Profiles let you keep different settings per environment: application-dev.properties and application-prod.properties, activated with spring.profiles.active=dev (or the SPRING_PROFILES_ACTIVE environment variable). Read your own settings with @Value("${app.gst-rate}") or, better for groups of settings, a @ConfigurationProperties record.

Actuator

spring-boot-starter-actuator adds production endpoints such as /actuator/health (is the app and its database up?) and metrics. Load balancers and Kubernetes use the health endpoint to decide whether to send traffic. Expose only what you need, and protect the rest.

Careful: Classes outside the main application class's package (for example a controller in com.other) are not found by component scanning, and their endpoints return 404 with no error at start-up. Keep everything under the base package. And never commit real passwords in application.properties; use environment variables or a secrets manager.
Think of it like this: Spring Boot is like moving into a fully furnished service apartment: electricity, Wi-Fi and kitchen are already set up (auto-configuration), you choose which extras you want (starters), and you can still replace the curtains (override a default) if you do not like them.
Tip: Check the current Spring Boot version and its requirements on start.spring.io and in the release notes; versions move quickly (Spring Boot 3.x and 4.x both need Java 17+).

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Creating a Spring Boot app · 1/6

Spring Boot

In this lesson you will create and run your first Spring Boot application.

Prefer a video by a teacher? Find free videos on “spring boot tutorial for beginners start.spring.io application.properties profiles” on YouTube ↗

In real life: A product catalogue service in an afternoon

A retail chain needed a small internal API to list products for its store tablets. A developer generated a Spring Boot project with Web, Data JPA and PostgreSQL, wrote one entity, one repository and one controller, and had it running in a container the same afternoon, with /actuator/health wired to the company's monitoring.

ini
# application-prod.properties
spring.datasource.url=${DATABASE_URL}
management.endpoints.web.exposure.include=health,info
server.shutdown=graceful

Practice (0/3 done)

Task 1 · Question

Which three annotations does @SpringBootApplication combine?

Task 2 · Question

Your new @RestController in package com.other is never found (404), but the app is in com.shop.catalog. Why?

Task 3 · Practice

Generate a Spring Boot project on start.spring.io with Spring Web and Actuator, run it, and open http://localhost:8080/actuator/health. Then add application-dev.properties with server.port=8081 and run with the dev profile active.

Quick quiz

Question 1

1.What is a Spring Boot starter?

Question 2

2.Which server is embedded by default with spring-boot-starter-web?

Question 3

3.What is the minimum Java version for Spring Boot 3?

Question 4

4.What does /actuator/health report?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What is a Spring Boot starter?

Answer: A dependency that brings a tested set of libraries for one purpose

For example spring-boot-starter-web.

Which server is embedded by default with spring-boot-starter-web?

Answer: Tomcat

Embedded Tomcat on port 8080.

What is the minimum Java version for Spring Boot 3?

Answer: Java 17

Spring Boot 3 requires Java 17+.

What does /actuator/health report?

Answer: Whether the application (and its dependencies like the database) is up

Used by load balancers and monitoring.

What is the default scope of a Spring bean?Related

Answer: Singleton: one shared instance per container

Singleton by default.

From Spring and dependency injection

Which package do JPA annotations come from in Spring Boot 3 and later?Related

Answer: jakarta.persistence

The move to Jakarta EE renamed the packages.

From JPA and Spring Data repositories

What is usually the most important skill set for a Java backend role?Related

Answer: Core Java, Spring Boot, SQL and REST

Most job posts list these together.

From Projects and a Java career

Which file shows the kernel arguments of the current boot?Related

Answer: /proc/cmdline

/proc/cmdline.

From The boot process, step by step

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 3 more topics in “Spring Boot basics” before its exam: Building a REST API, Layers: controller, service and repository, JPA and Spring Data repositories.
  2. Module 11 exam: Spring Boot basics · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Design patterns, JVM internals and interviews. 60 topics left in the course before the final exam.

← Previous: Spring and dependency injection