Creating a Spring Boot application is simple and fast due to Spring Boot's built-in features like auto-configuration and starter dependencies. Here's a step-by-step guide to creating a Spring Boot application:
1. Set Up the Project:
Using Spring Initializr (Recommended):
Spring Initializr is a web-based tool to generate a Spring Boot project with pre-configured settings.
- Go to the Spring Initializr website.
- Configure your project settings:
- Project: Maven Project (or Gradle Project).
- Language: Java (or Kotlin/Groovy).
- Spring Boot Version: Choose the latest stable version (e.g., 2.7.0 or later).
- Project Metadata: Set the group (e.g.,
com.example
) and artifact (e.g.,myapp
) names. - Packaging: Jar (or War if you're deploying to an external server).
- Java Version: Select your preferred version (e.g., 8, 11, or 17).
- Select Dependencies:
-
Add relevant dependencies, such as:
- Spring Web: To build a web application with Spring MVC.
- Spring Data JPA: For database access using JPA.
- H2 Database: For an in-memory database.
- Spring Boot DevTools: For hot reloading during development.
- Click Generate to download the project as a ZIP file.
- Extract the ZIP file to your workspace.
Manual Setup Using Maven or Gradle:
You can also create the project manually using Maven or Gradle.
- Maven: Create a
pom.xml
file. - Gradle: Create a
build.gradle
file.
Example pom.xml for Maven:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>myapp</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- Spring Web dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Data JPA dependency --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
<!-- H2 in-memory database –>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!-- DevTools for hot reloading –>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. Write the Main Application Class:
Spring Boot applications are standalone and start from the main() method, which runs the application using SpringApplication.run()
.
In the src/main/java/com/example/myapp
folder, create a class annotated with @SpringBootApplication
.
package com.example.myapp; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
@SpringBootApplication
: This annotation combines@Configuration
,@EnableAutoConfiguration
, and@ComponentScan
, and it enables Spring Boot's auto-configuration and component scanning features.
3. Create a REST Controller (Optional):
In a web application, you might want to create a REST controller to handle HTTP requests.
In the src/main/java/com/example/myapp
folder, create a controller class.
package com.example.myapp; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, World!";
}
}
@RestController
: This annotation is used to define a REST controller in Spring Boot.@GetMapping("/hello")
: Maps HTTP GET requests to the/hello
endpoint.
4. Configure the Application (Optional):
Spring Boot uses application.properties or application.yml files to configure the application. By default, this file is located in the src/main/resources
directory.
Example application.properties
:
# Set server port server.port=8081 # H2 Database configuration
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.h2.console.enabled=true
- You can change the server port and configure your data source in this file.
- H2 console can be accessed at
/h2-console
if enabled.
5. Run the Application:
Using an IDE (IntelliJ, Eclipse, etc.):
- Open the project in your favorite IDE.
- Locate the
MyApp
class that contains themain()
method. - Right-click on the class and select Run or Debug.
Using Maven or Gradle:
- If you're using Maven, navigate to the project directory and run:
bash
mvn spring-boot:run
* If you're using Gradle, run:
bash
gradle bootRun
Running the JAR:
Once the application is built, you can package it as a JAR and run it:
mvn clean package java -jar target/myapp-1.0.0.jar
6. Access the Application:
Once the application is running, you can access it in a web browser.
- For the above controller example, visit:
http://localhost:8081/hello
- You should see the response:
"Hello, World!"
7. Additional Steps (Optional):
- Add a Database: Add Spring Data JPA and a database driver like MySQL or PostgreSQL to persist data.
- Enable Security: Add Spring Security for authentication and authorization.
- Deploy the Application: Package the application as a JAR or WAR and deploy it to a server like Tomcat or run it as a standalone service.
Summary:
- Spring Initializr: Use it to generate a project with all necessary dependencies.
- Main Class: Create a main class annotated with
@SpringBootApplication
and include amain()
method. - Controllers: Define controllers to handle HTTP requests.
- Configuration: Use
application.properties
orapplication.yml
to configure the application. - Run the Application: Run it using an IDE, Maven/Gradle, or a packaged JAR file.
With these steps, you can create, configure, and run a Spring Boot application.