Expert · Spring Boot basics · topic 52 of 60 · 25 min
Creating a Spring Boot app
Explanation
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
- Open start.spring.io (Spring Initializr).
- Choose Maven or Gradle, Java, a current Spring Boot version, and Java 21 (or the LTS you use).
- Group com.shop, artifact catalog.
- Add dependencies: Spring Web, Validation, Spring Data JPA, and a database driver (PostgreSQL, or H2 for practice). Optionally Spring Boot Actuator.
- Generate, unzip, and open the folder in your IDE.
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);
}
}./mvnw spring-boot:run # or run main() from the IDE
# … Tomcat started on port 8080 (http)
# … Started CatalogApplication in 1.8 secondsStarters 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
# 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=18Profiles 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.
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
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.
# application-prod.properties
spring.datasource.url=${DATABASE_URL}
management.endpoints.web.exposure.include=health,info
server.shutdown=gracefulPractice (0/3 done)
Which three annotations does @SpringBootApplication combine?
Your new @RestController in package com.other is never found (404), but the app is in com.shop.catalog. Why?
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Building a REST APISpring Boot basics · same module
- Layers: controller, service and repositorySpring Boot basics · same module
- Projects and a Java careerDesign patterns, JVM internals and interviews
- The boot process, step by stepLinux and Red Hat (RHCSA to RHCE) · Boot, recovery and containers
- Docker Compose: multi-container appsDevOps: Git, Docker, Kubernetes and CI/CD · Docker
- Popular stacks and how to chooseFull-Stack Development · What full-stack development is
Questions people ask about this
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.
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.
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.
Which file shows the kernel arguments of the current boot?Related
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
- 3 more topics in “Spring Boot basics” before its exam: Building a REST API, Layers: controller, service and repository, JPA and Spring Data repositories.
- Module 11 exam: Spring Boot basics · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Design patterns, JVM internals and interviews. 60 topics left in the course before the final exam.