Spring Boot Setup in Java
Introduction
Spring Boot revolutionized Java enterprise development by eliminating boilerplate configuration and providing an opinionated approach to building production-ready applications. While traditional Spring required extensive XML or Java configurations to wire beans and set up servers, Spring Boot introduces auto-configuration and embedded servers (like Tomcat).
This guide walks through the process of setting up a new Spring Boot application from scratch.
1. Initializing the Project
The most common and recommended way to bootstrap a Spring Boot application is using the Spring Initializr.
Using Spring Initializr (Web)
- Go to
start.spring.io. - Project: Maven or Gradle.
- Language: Java.
- Spring Boot Version: Choose the latest stable release (e.g., 3.x).
- Project Metadata:
- Group:
com.example - Artifact:
demo - Java Version:
17or21
- Group:
- Dependencies: Add
Spring Web(to build REST APIs). - Click Generate to download the
.zipfile, extract it, and open it in your IDE (IntelliJ IDEA, Eclipse, or VS Code).
Using Spring Boot CLI
If you prefer the command line:
spring init --dependencies=web --build=maven --java-version=17 my-project
2. Project Structure
Once imported into your IDE, a typical Maven-based Spring Boot project looks like this:
my-project
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── demo
│ │ └── DemoApplication.java
│ └── resources
│ ├── application.properties
│ ├── static
│ └── templates
└── test
└── java
└── com
└── example
└── demo
└── DemoApplicationTests.java
pom.xml: Contains the dependencies, specifically thespring-boot-starter-parentandspring-boot-starter-web.application.properties/.yml: Centralized configuration file for server ports, database connections, etc.DemoApplication.java: The main entry point of the application.
3. The Main Application Class
The entry point of a Spring Boot application is a class annotated with @SpringBootApplication.
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);
}
}
Under the Hood: @SpringBootApplication
This annotation is actually a convenience annotation that combines three powerful features:
@EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.@ComponentScan: Tells Spring to look for other components, configurations, and services in the current package and its sub-packages.@Configuration: Tags the class as a source of bean definitions for the application context.
4. Creating Your First REST Controller
To verify the setup, let's create a simple REST API. Create a new class HelloController.java in the same package (or a sub-package like com.example.demo.controller).
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring Boot!";
}
}
@RestController: Marks the class as a web controller capable of handling HTTP requests and directly returning data (JSON/text) instead of rendering views.@GetMapping: Maps HTTP GET requests to/helloto this specific method.
5. Configuration (application.properties)
Spring Boot comes with sensible defaults (e.g., embedded Tomcat runs on port 8080). You can override these in src/main/resources/application.properties.
# Change the default server port
server.port=9090
# Set an application name
spring.application.name=MyDemoApp
6. Running the Application
There are multiple ways to run your Spring Boot application:
Via IDE
Simply run the main method in DemoApplication.java.
Via Maven
Open a terminal in the project root and run:
./mvnw spring-boot:run
Once started, open a browser or use tools like curl or Postman to visit http://localhost:9090/hello. You should see Hello, Spring Boot!.
7. Next Steps
With the basic setup complete, you can expand your application by adding:
- Spring Data JPA: For database interactions.
- Spring Security: For authentication and authorization.
- Actuator: For production-ready monitoring and metrics.