Spring Framework Tutorials for Beginners, Spring Framework Step by Step, Hello World Example in Spring Framework with Eclipse, web.xml in Spring Framework


Spring Framework Tutorials for Beginners - Spring Framework Step by Step, web.xml in Spring Framework.


Hello World Example in Spring Framework with Eclipse

Hello and Welcome to all who are new in Spring Framework. I am going to give a quick overview about spring framework and how to make a simple Example like "Hello World" Example......

I am Using Eclipse IDE for this example and we need the Spring Framework JAR's from so download these JAR's from spring official site and Apache commons-logging-1.1.1-bin.zip  from apache official site.

Now First we have to create a new Project in Eclipse IDE. Click on  New from Toolbar --> Other..-->choose Java Project.




Give it a name like HelloWorld.
click on finish button.

then first we want to add the external Spring JAR's which we have downloaded in first step. right click on your HelloWorld project select properties like this


then the property window will appear like this and choose java build path
then click on Libraries tab then click on Add Library then the Add Library window will appear then choose User Library like this


click on Next then click on User Libraries then a User Libraries window will appear.


then click on New then a new window will appear with the name New User Library give it a name like Spring like this.


then click on Ok then click on Spring Liabrary which is appear now in your User Libraries then click on Add External JAR's then select the downloaded JAR's form your Computer's drives like this
first we will add the Spring JAR's from lib folder under the spring-framework-3.2.0.RELEASE


Select all JAR's from there and open them then the all JAR's will be available under the Spring Library
 then again add the Apache commons.logging JAR like this


Select the only one JAR commons-logging-1.1.1 from commons-logging-1.1.1 folder which you have downloaded in first step.
Now you are ready to develop a Spring project Now right click on your project HelloWorld then click on New -->Class



then give a name to your Class and give package name as well like this 


I have given the package name W3STutorial and Class name HelloWorldExample
Now create a new class by the name Message under the W3STutorial the code for each class are as follows.....


HelloWorldExample.java

package W3STutorial;


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

public class HelloWorldExample {

public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Message message=(Message)context.getBean("message");
message.showMessage();

}

 

  
Message.java



package W3STutorial;

public class Message {
private String msg;

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}
public void showMessage()
{
System.out.println(msg);
}

}


then Create a XML file like this....... right click on src folder under your project and click on New-->Other...-->XML-->XML File and then Next and give it a name Spring.xml.....

                         

under the Spring.xml write this code...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
    "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
    <beans>
    <bean id="message" class="W3STutorial.Message">
    <property name="msg" value="Hello Wold"/>
    </bean>
    
    
    </beans>


Now your first Spring project is ready to Run this Project right Click on your project and then Run As then click on Java Application like this



 if everything is Ok then your project run successfully the message shown as follows









the Hello World Message show in bottom of your IDE...


So I wish now you are ready to make your first Spring Project if you want to know more about Spring then post your comments here.
All the Best




You Might Also Like







{ 2 comments... read them below or add one }

Anonymous said...

ApplicationContext cannot be resolved to a type
ClassPathXmlApplicationContext cannot be resolved to a type
MyEvent cannot be resolved to a type

Admin said...

check the Spring Framework JAR files are available on your project library...

Post a Comment