How to create a basic Spring 6 project using Maven

  • 4.0/5
  • 4470
  • Jul 20, 2024

Below is a step-by-step guide to creating a basic Spring project using Maven.

1) Create a Maven Project

Use the following Maven command to create a new Maven project.

mvn archetype:generate -DgroupId=com.tb -DartifactId=spring-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

2) Import in IntelliJ IDEA

If you haven't already, open IntelliJ IDEA on your system. Go to "File" > "New" > "Project from Existing Sources...".

In the file dialog, navigate to the directory where your Maven project is located. Select the pom.xml file within the project directory and click "Open."

3) Update pom.xml

In total, the application requires the below-mentioned dependencies:

4) Create Spring Configuration

Create a Java configuration class that uses annotations to define your Spring beans and their dependencies. This class should be annotated with @Configuration.

5) Create the Main Application

Create a main application class where you'll bootstrap the Spring context using AnnotationConfigApplicationContext.

AnnotationConfigApplicationContext is an implementation of the ApplicationContext interface in Spring. It allows you to create a Spring application context based on Java configuration classes that use annotations to define beans and their dependencies.

6) Build the Project

You can use the following Maven command to compile it.

mvn compile

7) Run the Application

Click on the green "Run" button in the gutter (left-hand side) of the editor window next to the main method.

The Java application will start running, and you should see the output in the "Run" tab at the bottom of the IntelliJ IDEA window.

Employee Name: Honest Employee

Source code: GitHub

Index
How to Implement PostgreSQL Full-Text Search with Spring Boot

15 min

Spring's transaction management with the @Transactional annotation

9 min

Spring Boot Rest APIs with PostgreSQL (Spring Boot + Rest APIs)

15 min

Caching in Spring Boot (@Cacheable, @CacheEvict & @CachePut)

21 min

Declarative REST Client in Spring Boot (Spring 6 HTTP Interface)

13 min

Profiling a Spring Boot application with Pyroscope

7 min

Service discovery in Spring Boot (Spring Cloud + Netflix Eureka)

9 min

Dockerize Spring Boot app and Push image to DockerHub (Spring Boot + DockerHub)

4 min

Creating a Jenkins Pipeline for Spring Boot application

2 min

Circuit Breaker Pattern in Microservices (Spring BOOT + Resilience4j)

4 min

Edge Server Pattern in Microservices (Spring Cloud Gateway)

7 min

Monitoring Microservices (Spring Boot + Micrometer + Prometheus + Grafana)

7 min

Spring Cloud config server setup with Git

8 min

Distributed Tracing in Microservices (Spring Cloud Sleuth + Zipkin)

9 min

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

24 min

Deploying Spring Boot microservices on Kubernetes Cluster

12 min

Reactive programming in Java with Project Reactor

50 min

Spring Reactive with PostgreSQL (Spring Boot WebFlux + PostgreSQL)

13 min

Spring Reactive, Thymeleaf Hello World (Spring Webflux + Thymeleaf + JS/CSS)

9 min

Problem JSON (application/problem+json) in Spring WebFlux

15 min

Spring Boot Login/Logout (Spring Security + MySql + Thymeleaf)

21 min

Securing Server-to-Server Communication with "Spring Boot" & "OAuth 2"

18 min

Sending Emails in Spring Boot via SMTP

7 min

How to create a basic Spring 6 project using Maven

5 min

Spring Boot, Thymeleaf Hello World (Spring Boot + Thymeleaf + JS/CSS)

9 min