Monday, August 13, 2018

Spring Hello World Example with Maven

Step  1:Create Maven Project

Create maven project and give name  as springExamples.

Note:Use eclipse for this and install maven plugin to get started.YOu can easily get steps of installation on google .just type" m2e plugin for eclipse" in google.

Step 2:  Maven Project Structure

Your folder structure will have pom.xml file .

Step 3: Spring Dependencies

We will need spring jar to start .

Add below dependency in pom.xml

org.springframework

spring-core

4.1.6.RELEASE

Step 4: Maven Clean Install

Run Maven clean install so it will download Jar from maven repository.Run this from command prompt using command mvn clean install or you can run it from eclipse

After running this your project will have  spring.jar

You will able to see spring jar.

Step 5:HelloWorld.java

Create HelloWorld.java class

File:HelloWorld.java

public class HelloWorld {

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public void display ()

{

System.out.println(this.getName());

}

}

Step 6: Create ApplicationContext.xml 

Create ApplicationContext.xml  file for spring configuration.

File:ApplicationCOntext.xml

"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-2.5.xsd">

"helloBean" class="HelloWorld">

"name" value="HelloWorld :I am able to run project" />

Step 7: Create SpringRunner.java

Create SpringRunner.java class to run this small spring application

File:SpringRunner.java

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringRunner {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

ApplicationContext context = new ClassPathXmlApplicationContext(

"ApplicationContext.xml");

HelloWorld obj = (HelloWorld) context.getBean("helloBean");

obj.display();

}

}

Step 8: Output

Right click in SpringRunner class and Run as Java Application

It will print

HelloWorld :I am able to run project

No comments:

Post a Comment

How to check whether operating system is 64 bit or 32bit?

What is 32 and 64 bit operating system? The terms 32-bit and 64-bit refer to the way a computer's processor that is CPU, handles info...