How to Install Spring Boot: Step-by-Step Guide
To install
Spring Boot, use the Spring Initializr website to generate a project with dependencies, or add spring-boot-starter dependencies to your build.gradle or pom.xml. Then import the project into your IDE and run the main application class.Syntax
Spring Boot projects are typically created using build tools like Maven or Gradle. You add starter dependencies to your build file to include Spring Boot features.
pom.xmlfor Maven: addspring-boot-starter-parentand dependencies inside<dependencies>.build.gradlefor Gradle: apply theorg.springframework.bootplugin and addimplementation 'org.springframework.boot:spring-boot-starter'.
xml
<!-- Maven pom.xml snippet -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.6</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>Example
This example shows how to create a simple Spring Boot application using Spring Initializr and run it.
java
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Output
2024-06-01 10:00:00.000 INFO 12345 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.345 seconds (JVM running for 3.456)
Common Pitfalls
- Not using the correct Spring Boot version in your build file can cause compatibility issues.
- Forgetting to add the
@SpringBootApplicationannotation on the main class prevents the app from starting properly. - Running the project without importing dependencies or refreshing the build tool causes errors.
java
<!-- Wrong: Missing @SpringBootApplication --> public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } // Correct: import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.SpringApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Quick Reference
Use Spring Initializr at start.spring.io to quickly generate a Spring Boot project. Choose your build tool (Maven or Gradle), Java version, and dependencies, then download and import into your IDE.
Alternatively, add these dependencies manually to your build file and create a main class with @SpringBootApplication.
Key Takeaways
Use Spring Initializr to quickly create a Spring Boot project with needed dependencies.
Add spring-boot-starter dependencies in your Maven or Gradle build file to include Spring Boot features.
Annotate your main class with @SpringBootApplication to enable auto-configuration and component scanning.
Import the generated project into your IDE and run the main method to start the application.
Check your Spring Boot version compatibility to avoid build or runtime errors.