You are here: Home / Topics / How did I setup my First Spring Application in IntelliJ Idea with Code Examples

How did I setup my First Spring Application in IntelliJ Idea with Code Examples

Filed under: Spring on 2023-08-19 13:10:50

Hello developers,

In this post, I want to tell you how did i setup my first Spring application in IntelliJ Idea. What errors did I faced and How did I overcome them. You have any question regarding setup of your application you can come to comment section. I will try to help you in that. 

My Name is Shyam and I am a Java, Spring Developer.

Let's start …….

First I would like to tell from where did i start it…..

I opened IntelliJ idea and created one project example in Language Java and Build System as IntelliJ 

I created one package with name com.example.di

I created on class Student with one property int i. I created Setters and Getters for this property and created a method cheat() which was just printing that I am cheating……

Step 2: 

I created a beans.xml file and placed it in my package folder. Code of the beans.xml file is written below. 

Step 3: 

I created one Main class with main() method where i created ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

 

It was displaying with red line below. When i hover mouse on it, I came to know that i have to import packages for these classes so I imported two packages.

1. import org.springframework.context.ApplicationContext;
2. import org.springframework.context.support.ClassPathXmlApplicationContext;

After that I created one object by 

Student stu = context.getBean("stu", Student.class");

stu.cheat();

I hoped that i will get the output of cheat() method of Student Class. But……………..

I got a lot of errors in my console. 


I downloaded spring jar files and added in my project library (If you don't know how to add jar files in IntelliJ Idea, Follow these steps) these files are as follows:

spring-aop-6.0.11.jar
spring-beans-6.0.11.jar
spring-context-6.0.11.jar
spring-core-6.0.11.jar
spring-expression-6.0.11.jar
spring-jcl-6.0.11.jar

These files were found online on google.

But again i was seeing errors in my console so i again searched google for these problems and i found the final solution.
I have to tell maven to load dependencies which are useful for spring frame work.

I searched google for all those problems and then I come to the following points to avoid errors
I followed the below route and it started working......

Step - 1
for using spring framework we have to make sure we are creating project with maven

Step - 2

Structure of My Files

 

File Name: beans.xml


Note: keep this file in Resources folder of your application.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

   <!-- bean definitions here -->

   <bean id="stu" class="org.example.Student">
       <property name="i" value="1"/>
   </bean>

</beans>


File Name: Student.java

 

package org.example;

public class Student {
   private int i;

   public void setI(int i){
       this.i = i;

   }


   public int getI() {
       return i;
   }

   public void cheat(){
       System.out.println("Student Cheatin started.....");
   }
}


File Name: pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
   <artifactId>springfirst</artifactId>
   <version>1.0-SNAPSHOT</version>

   <properties>
       <maven.compiler.source>20</maven.compiler.source>
       <maven.compiler.target>20</maven.compiler.target>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

   </properties>

   <dependencies>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-core</artifactId>
           <version>5.3.14</version>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>5.3.14</version>
       </dependency>

       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-test</artifactId>
           <version>5.3.14</version>
           <scope>test</scope>
       </dependency>
   </dependencies>

</project>


and Finally 
File Name: Main.java

package org.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
   public static void main(String[] args) {

       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
       Student student = context.getBean("stu", Student.class);
       student.cheat();
   }
}


After adding these dependencies i refresh maven , maven clean and install and run Main() method of my application which contains the following code.

And you can see the output as below in your console.


Output:


Student Cheatin started.....

Process finished with exit code 0

 

About Author:
M
Mr. Dubey     View Profile
Founder and CEO of MCQ Buddy. I just like to help others.