Setting Up a Spring Project

Setting up a Spring project correctly is essential to ensure smooth development and maintainability.

Prerequisites (JDK, IDE, Maven/Gradle)

Before starting a Spring project, ensure we have the necessary tools installed on our system.

1.1 Java Development Kit (JDK)

Spring applications require JDK 8 or later (JDK 17+ recommended for latest features).

Check if Java is installed:

java -version

Download JDK:

1.2 Integrated Development Environment (IDE)

You need an IDE for writing and managing your Spring project. Some popular choices:

  • IntelliJ IDEA (Recommended) – Excellent Spring Boot support

  • Eclipse IDE – Widely used for Java development

  • VS Code – Lightweight alternative with Java extensions

  • Spring Tool Suite (STS) – Optimized for Spring projects

1.3 Build Tools: Maven or Gradle

Spring projects typically use Maven or Gradle as the build system.

Check Maven installation:

mvn -version

Check Gradle installation:

gradle -version

Install Maven/Gradle if not available:

Creating a Spring Project (Spring Initializr)

Spring Initializr is the easiest way to generate a Spring Boot project.

  1. Select:

  • Project Type: Maven / Gradle

  • Language: Java

  • Spring Boot Version: Latest stable version

  1. Enter Group ID (e.g., com.example) and Artifact ID (e.g., myapp).

  2. Add required dependencies (e.g., Spring Web, Spring Boot Actuator, Spring Data JPA).

  3. Click "Generate" to download the project as a ZIP file.

  4. Extract and open the project in your preferred IDE.

2.2 Creating a Project Manually

If we prefer, we can create a project manually using Maven or Gradle.

Maven Project Setup

mvn archetype:generate -DgroupId=com.example -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Add Spring Boot dependencies in pom.xml:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Run the project:

mvn spring-boot:run

Gradle Project Setup

Initialize a Gradle project and add dependencies to build.gradle:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
}

Run the project:

./gradlew bootRun

Project Structure Overview

Once the Spring project is created, the default structure follows a standardized convention. Below is an overview of key directories and files:

myapp/
│── src/
│   ├── main/
│   │   ├── java/com/example/myapp/
│   │   │   ├── MyAppApplication.java   # Main entry point of the application
│   │   │   ├── controller/             # Controllers (handling HTTP requests)
│   │   │   ├── service/                # Business logic
│   │   │   ├── repository/             # Data access layer
│   │   │   ├── model/                  # Data models
│   │   ├── resources/
│   │   │   ├── application.properties  # Configuration file
│   │   │   ├── static/                 # Static files (HTML, CSS, JS)
│   │   │   ├── templates/              # Templates (Thymeleaf, Freemarker)
│   │   │   ├── schema.sql              # Database initialization scripts
│   ├── test/java/com/example/myapp/    # Unit and integration tests
│── pom.xml (for Maven projects)  
│── build.gradle (for Gradle projects)
│── mvnw, mvnw.cmd (Maven wrapper)
│── gradlew, gradlew.bat (Gradle wrapper)
│── .gitignore (Git ignore rules)

Important Files and Folders

Folder/File
Description

MyAppApplication.java

Main class annotated with @SpringBootApplication. Starts the Spring application.

controller/

Contains REST controllers (@RestController) that handle HTTP requests.

service/

Business logic layer (@Service).

repository/

Data access layer (@Repository). Works with databases via Spring Data JPA.

model/

Contains entity classes (@Entity) for database interactions.

resources/application.properties

Configuration file for database connections, server settings, etc.

resources/templates/

Used for template engines like Thymeleaf or Freemarker.

test/java/

Contains JUnit test cases for unit and integration testing.

Running the Spring Boot Application

Once the project is set up, run it using:

mvn spring-boot:run   # If using Maven
./gradlew bootRun      # If using Gradle

Or directly from the main method:

@SpringBootApplication
public class MyAppApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyAppApplication.class, args);
    }
}

The application will start at http://localhost:8080/.

Last updated

Was this helpful?