Home Post Spring Framework

How to create a basic Spring 6 project using Maven

Mar 31, 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

avatar

NK Chauhan

NK Chauhan is a Principal Software Engineer with one of the biggest E Commerce company in the World.

Chauhan has around 12 Yrs of experience with a focus on JVM based technologies and Big Data.

His hobbies include playing Cricket, Video Games and hanging with friends.

Categories
Spring Framework
Microservices
BigData
Core Java
Java Concurrency