Spring Hello World And Environment Setup

In my previous blog I wrote in details How to create Maven Project in Eclipse. Today in this article we will do environment setup for spring 4 and we will also do a Spring Hello World project to have a better understanding of Spring framework.

Prerequisite:

  • Basic knowledge of Eclipse IDE.
  • Good knowledge of Core Java.

Environment Setup for Spring Framework

Setup JDK (Java Development Kit):

  • You can download the latest JDK from here.
  • You will get all the details to install the JDK in your system.

After installing JDK in your system, you need to set the Environment Variables.

Environment variables basically are strings that contain information  which tells where Java Development Kit (JDK) is installed on your system, which is used for executing Java.

To set the environment variable go to File Explorer -> Right click on “This PC” ->Click on “Advance system setting” -> Click on “Environment variables”.

spring hello world

Environment Variable setup-1

 

spring hello world

Environment variable setup – 2

Click on new of “User variable” to to set new environment variable.Below are the Environment variable which we need to set:

JAVA_HOME: D:\Abhinav Data\Softwares\jdk1.8.0_51

JDK_HOME : %JAVA_HOME%

JRE_HOME: %JAVA_HOME%\jre

CLASSPATH: ;%JAVA_HOME%\lib; %JAVA_HOME%\jre\lib;

PATH: ; %JAVA_HOME%\BIN;

I have set this environment variable according to my system. Now we are done with the environmental setup for JDK or Java environment. Now you can check, whether setup is done properly or not by compiling or running a simple java program.

Spring Hello World:

  • After creating the Maven project, your folder structure look like below:
spring hello world

Maven Project Folder Structure

  • Now we have to add spring framework dependencies in the pom.xml file. Below i have shared the content:

  • Now we have added the dependencies of spring 4 in pom.xml. Maven will automatically download all the necessary jars when we will run the code first time.
  • Now we will do the actual code for the Spring Hello World example.
  • Now create the package inside the src/main/java folder for the best coding practice.
  • Now we will create a bean class named as HelloWolrdDemo. Below is the content of the bean class:

Now we have to create a XML file to register our bean. Create XML file into resource folder.

spring hello world

beans.xml

In that XML file we give unique id to each bean.

When Spring application gets loaded into the memory, Framework makes use of the above configuration file(XML file) to create all the beans defined and assign them a unique ID as defined in <bean> tag.

You can use <property> tag to pass the values of different variables used at the time of object creation.

You can give any name to XML file(In my case it is beans.xml). Below is the content of Beans.xml file:

Now we will create Main app file from where we will run out project and in which we have our public static void main(String[] args) function.

Create java file under src/main/java with the name app.java. You can give any name to the file.

spring hello world

Below is the content of app.java file:

  • Here we have used ClassPathXmlApplicationContext() API. This API loads beans configuration file and it takes care of creating and initializing all the objects i.e. beans mentioned in the configuration file.
  • We have used getBean() method of the created ApplicationContext API. This method uses bean ID to return a generic object which finally can be casted to actual object. Once you have object, you can use this object to call any class method.

We are done with the coding part of the spring – Hello World project.

Run Spring Hello World Project:

  • Right click on app.java->Run As -> Java Application.
  • I also attached the screenshot below.
spring hello world

Output

 

Congratulations! If you reach this point means you have followed all the steps correctly and Sping Hello World project is running fine. If you are a beginner as a java developer and learning java, I recommend go through below android tutorials to get started.

If I missed any point or you have any doubt regarding this blog just let me know in the comment section.

If you like my article(Spring Hello World) please like our Facebook page and also follow us on Twitter so that you get regular updates. For regular updates you can also subscribe to www.hackpundit.com with your email.

Please also share on Facebook and Twitter to help other android developers. Thank You..!