Home Spring Spring3 Develop Hello World example using Spring 3.0 Framework
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Develop Hello World example using Spring 3.0 Framework

Advertisement
Hello World Example in Spring 3.0 Framework.

Spring 3 Hello World Example

     

In this section we will download spring, create new project in Eclipse IDE and then write simple Hello World application. We will finally run the application in the Eclipse IDE.

In this section you will learn how to download, create a new project in Eclipse IDE, add Spring 3.0 library files, the write simple program. Finally we will test the example code in Eclipse IDE.

Let's start developing "Hello World" example code

Step 1:

The Spring 3.0 at least requires JDK 5. So, make sure you have JDK 5 or above. Open dos prompt if you are using windows and type java -version. This will display the version of Java installed on your machine as shown below:

C:\>java -version
java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
Java HotSpot(TM) Client VM (build 14.3-b01, mixed mode, sharing)

C:\>

 If you don't know how to install Java read it at http://www.roseindia.net/java/index.shtml

Step 2:

Download Eclipse IDE from Eclipse download site at http://www.eclipse.org/downloads/. Installing Eclipse IDE is easy task, just extract the downloaded file and you will find the eclipse.exe in the extracted folder. To run the IDE, double click on the eclipse.exe file. Read more at http://www.roseindia.net/IDE/Eclipse/index.shtml.

Step 3:

Download the latest version of Spring 3 from http://www.springsource.org/download. For this tutorial we have downloaded spring-framework-3.0.0.RELEASE-with-docs.zip, which contains the documentation also. After extracting the file we got the following directories:

Step 4:

Now we will create a new project in Eclipse IDE and then add the library files.

Click next. Give any name to the project, say "Spring 3" and then click on the "Finish" button.

Eclipse will create a new project.

Step 5:

Create a new folder "lib" in the project space to hold the Spring 3.0 libraries. Right click on the "Spring3" in the project explorer and then select new folder option as shown below:

Then in the next screen enter "lib" next to the "Folder name" text field and click on the "Finish" button.

Step 6:

Now we will add the Spring 3. libraries to the project. Extract the "spring-framework-3.0.0.RELEASE-with-docs.zip" file if you have not extracted. Now go to the "dist" directory of the and then copy all the jar files (Ctrl+C) and paste on the lib directory (of our project) in the Eclipse IDE.

 

Then find the commons-logging.jar from extracted folder and also copy this file into Eclipse IDE. You will find this library into spring-framework-3.0.0.RELEASE-with-docs\spring-framework-3.0.0.RELEASE\projects\spring-build\lib\ivy folder.

Step 7:

Now all all the libraries to "Java Build Path". Right click on the "Spring3" in project explorer and then select properties. Then select "Java Build Path" --> Libraries and then click on the "Add JARs" button. And add all the libraries to Java Build Path.

 

Then click on the "OK" button. This will add all the libraries to the project. Now we can proceed with our Spring 3 Hello World example.

Step 8:

Create a new package net.roseindia to hold the java files. Right click on the "Spring3" and then select New --> Package. Then provide the package name as net.roseindia and click on the "Finish" button.

 

Step 9:

Create a new Java file Spring3HelloWorld.java under the package net.roseindia and add the following code:

package net.roseindia;

public class Spring3HelloWorld {
public void sayHello(){
System.out.println("Hello Spring 3.0");
}
}
In the above class we have created a method sayHello() which prints the "Hello Spring 3.0" on the console. In this section we will use the Spring framework to manage the Spring3HelloWorld bean, and then get the bean from the Spring runtime environment (Spring context) and the call the sayHello() method. In the next step we will xml file which will be used as Metadata to configure the bean.

Step 10:

Now create a new xml (SpringHelloWorld.xml) file using Eclipse IDE.

 

Add the following code to the xml file:

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

<bean id="Spring3HelloWorldBean"
class="net.roseindia.Spring3HelloWorld" />

</beans>
The above xml file declares the spring bean "Spring3HelloWorldBean" of the class net.roseindia.Spring3HelloWorld. The <bean .../> tag is used to declare a bean in the xml file. Spring uses the xml file to configure the spring run-time environment. Spring framework manages the beans in our program. In the next sections we will learn Spring core components in detail. Note: You should move the SpringHelloWorld.xml file into src directory of the project. Jut use mouse to drag and dop in the src folder.

 

Step 11:

Now create a java file (Spring3HelloWorldTest.java) into net.roseindia package and add the following code:

package net.roseindia;

import java.util.Map;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.Assert;

public class Spring3HelloWorldTest {

public static void main(String[] args) {

XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
"SpringHelloWorld.xml"));

Spring3HelloWorld myBean = (Spring3HelloWorld) beanFactory
.getBean("Spring3HelloWorldBean");
myBean.sayHello();
}
}	
In the above code we have created the instance of XmlBeanFactory and the retrieved the "Spring3HelloWorldBean". Then we can call the sayHello() method on the bean. The XmlBeanFactory  class is extension of DefaultListableBeanFactory that reads bean definitions from an XML document. In our case it reads the bean definitions from SpringHelloWorld.xml file.

Step 12:

To run the code in Eclipse open Spring3HelloWorldTest.java in the editor and then right click and select Run as --> Java Application. This execute the Spring3HelloWorldTest.java file and following output will be displayed in the console.

Jan 1, 2010 6:49:57 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [SpringHelloWorld.xml]

Hello Spring 3.0

 

In this section we developed Hello World example that uses the Spring 3.0. In the next section we will learn about @configuration, and see how this annotation can used in the Spring 3.0 application.

Download Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 1, 2010

Related Tutorials

Ask Questions?    Discuss: Develop Hello World example using Spring 3.0 Framework   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Rahul
April 12, 2011
Difference

How it made difference we can do it through java file also
Daniel Adigun
April 22, 2011
I'm having this bug. i need help please

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.<init>(DefaultSingletonBeanRegistry.java:83) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.<init>(FactoryBeanRegistrySupport.java:43) at org.springframework.beans.factory.support.AbstractBeanFactory.<init>(AbstractBeanFactory.java:176) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:155) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:166) at org.springframework.beans.factory.support.DefaultListableBeanFactory.<init>(DefaultListableBeanFactory.java:156) at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:77) at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:66) at net.roseindia.Spring3HelloWorldTest.main(Spring3HelloWorldTest.java:16)
spring learner
May 24, 2011
Spring 3 Hello World Example

Thanks very simple and efficient
tomi
May 25, 2011
doesn't work

hi, if i run the code,it throughs file not found exception...can't find the xml file
shyam
May 27, 2011
spring3 example code

good! it is really helpful!
stefano cirilli
June 5, 2011
Spring3HelloWorld example

Awesome application!
Pratik Soni
June 11, 2011
Great resource

A very useful page for new comers. Thanks..
Sky
June 25, 2011
Common Problem.

I think, the common problem is wrong location of .xml file. WARNING! You should put this file into the root folder of the SRC, not into the package folder.
deran
July 5, 2011
For java.lang.NoClassDefFoundError problem...

...i have solved it in this way. In my imported jar there is a file .libd and that file give me the java.lang.NoClassDefFoundError. Remove the import of this file and it should work.
nalin
August 26, 2011
helpful tutorial

nice one!! Please upgrade the tutorial to newer versions of Spring framework. XmlBeanFactory is no longer supported in latest releases of Spring3
Nico
August 30, 2011
Good Job

Nice example. Loved the illustrations. This helps me thrue those tuff first steps when learning something new.
JJJ
May 29, 2013
Not working

Not working
Sandeep Bansal
September 29, 2011
About the demo

This is a really a good and easy demo for learners. Thanks guys.
Saul
October 6, 2011
Wrong Code

To run this exercise you need to use the next code: ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"SpringHelloWorld.xml"}); BeanFactory factory=context; Spring3HelloWorld myBean=(Spring3HelloWorld)factory.getBean("Spring3HelloWorldBean"); myBean.sayHello(); }
Mike
February 7, 2013
No proper information

Clicking 'Next' to view next topic or 'Prev' link for previous topi is not working. Not informative
satish mahajan
October 8, 2011
Spring 3.0

Good Demo Yaar Nice One Keep it Up this demo was so good for me to start Spring 3.0 Frame Work one thing i world like to say plz kindly give littel bit more explanation about demo Thaks
Mark
October 9, 2011
Easy

This is very easy. But thank you all the same.
aruseth
October 11, 2011
hi

thanks alot
saji
November 2, 2011
nice way to start

Nice explanation
Paul Anekwe
November 7, 2011
Learning Spring

Hello, I have been trying to learn spring for as long as 3 months now. But all the online tutorials didn't really help. I will read and thing I understand, but when I try same thing using my netbeans 6.9 version, it will tell me that building failed. I read some considerable chapter of Spring in Action 3 (sia3), understood some XML configurations but sia3 didn't use web based examples, they used console. I mean, up to chapter 6, its still using core POJOs for its examples. Can you please help me. I need bit by bit, step by step advice on how I can build web app (from simple to complex) using spring framework. Thank you.
rob
November 7, 2011
navigation links not working?

Hello! The links "<< Previous Index Next >>" near the bottom do not work, i.e. they all point to this same page. So it's hard to find "the next section" of the tutorial. All the best, rob
Tengjiao
November 7, 2011
Good

Nice job!
Ratnesh Dubey
November 13, 2011
Unable to runn spring hello world exaple

I am using 3.1.0.RC1 and getting bellow mention error. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.<init>(DefaultSingletonBeanRegistry.java:83) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.<init>(FactoryBeanRegistrySupport.java:43) at org.springframework.beans.factory.support.AbstractBeanFactory.<init>(AbstractBeanFactory.java:175) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:160) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:171) at org.springframework.beans.factory.support.DefaultListableBeanFactory.<init>(DefaultListableBeanFactory.java:158) at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:77) at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:66) at springTest.Spring3HelloWorldTest.main(Spring3HelloWorldTest.java:14) Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 9 more please help Ratnesh Dubey
Joe J.
November 15, 2011
Needs common-logging-1.1.x.jar

Thanks for posting this intro. It helps a lot. With the latest spring 3.x source, I need to add the apache common-logging-1.1.x.jar file to the build to get even this simple example to run. Perhaps it's just me.
Russell
December 13, 2011
Problem with page navigation

Dear Sir, I thank you for keeping this website updated and running. I am a big fan of it. I would just like to draw your attention towards page navigation buttons "previous", "index", "next". They are not working properly. If I click on them, they return the same page. Please have a look and get it rectified. With best regards, Russell CA
ubedulla
December 19, 2011
spring hello example

good notes
Naveed
January 3, 2012
Spring

i need a jdbc example
Dalia Banerjee
January 4, 2012
NoClassDefFoundError

Hi, When I tried to run the code as stated by the Hello World example, I get the following error - java.lang.NoClassDefFoundError: net/roseindia/Spring3HelloWorldTest Caused by: java.lang.ClassNotFoundException: net.roseindia.Spring3HelloWorldTest at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) Exception in thread "main" I did everything exactly as stated in the example. please let me know what went wrong?
Hürol Türen
January 10, 2013
XmlBeanFactory is deprecated

XmlBeanFactory is deprecated in Spring Franework 3.2.0. ApplicationContext is used instead of XmlBeanFactory.
Atul
February 4, 2012
Spring 3.0 Tutorial

Nice and easy to start on Spring 3.0.
jonathan
February 5, 2012
Thanks

Thanks for this quick tutorial... just setting things up and this helped. P.S. Have you thought about using ReCaptcha?
Mark_D
February 8, 2012
Feedback on post

Firstly, I'd like to appreciate the efforts you have taken to explain this all...now coming to the feedback upon this is: The Tutorial is not useful as it contains incomplete information about what you are trying to explain, first i think you should go through the code you are providing for download and then you must read the article you wrote in here..... you will find that the information you have provided here is incomplete that leads to nothing but the confusion, so kindly review the articles and tutorials once again as per the latest tools and techniques and kindly provide with the proper information. Thank You.
radha
February 16, 2012
this is a good tutorial

thanks...........:)
Rajesh
February 20, 2012
Bean file location is not proper

the file should be placed on the ROOT of your CLASSPATH. If the file is in some other "directory" then it won't be found.
Anonymous
March 2, 2012
Error while running

Hi, It was very simple to follow and elegant to look at but when I tried running it, I got a java.lang.NoClassDefFoundError: net/roseindia/Spring3HelloWorldTest Caused by: java.lang.ClassNotFoundException: net.roseindia.Spring3HelloWorldTest Please help.
Anonymous
March 2, 2012
Error after solving classdefnotfound exception

Hi, I resolved the error with the classdefnotfound by removing one of the .libd files from the build path which got included along with the other jars. Now I get an error "Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'." . The only thing I changed is added ">" after <beans . I did that because the xml file complained that it required a ">" after "<beans which is not included in the xml file on the site.
hassan el-far
March 9, 2012
Question

how can we show hello world on a web page after??
Shailesh
March 20, 2012
Spring

This is really good example for starters. Thanks for your help.
shiva
April 3, 2012
spring

yes wat ever your are said is working good. Thank u Regaurds /shiva
Ram
April 23, 2012
Spring 3 Hello World Example

Short and sweet example..very good hands on for hello word!!
Magos Ho
May 3, 2012
re: Error 0x80010135:Path too long

Hi, when extracting the spring-framework-3.0.0.RELEASE-with-docs.zip, I got Error 0x80010135:Path too long for the ContextConfigurationWithPropertiesExtendingPro... Type PROPERTIES File, how can I fix that
Manoj Kumar
May 3, 2012
Learning Spring

Allway looking/finding for good solution from roseindia.net.
uMa
May 9, 2012
reply

thanks...it worked 4 me..
mohy
May 10, 2012
thanks

thanks you is very good it's help me very good
Alexander Püschel
May 14, 2012
Copy and Add

Where I have to copy the commons-logging.jar file? Into the lib-folder? And do I also add this file in the Java Build Path?
Morgan
May 14, 2012
Spring Hello World Example

I have been attempting to learn Spring and get a java project up and running. I am using your article to get it working, I've followed all the directions, when I am at step 12, I attempt to run it but I get an error in the console. May 14, 2012 11:52:30 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [SpringHelloWorld.xml] Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [SpringHelloWorld.xml]; nested exception is java.io.FileNotFoundException: class path resource [SpringHelloWorld.xml] cannot be opened because it does not exist at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:78) at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:66) at net.roseindia.Spring3HelloWorldTest.main(Spring3HelloWorldTest.java:12) Caused by: java.io.FileNotFoundException: class path resource [SpringHelloWorld.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) ... 4 more I hope that you will be able to help me.
Mike
May 19, 2012
Spring3 exampe

I tried following this example using spring 3.0.0 and eclipse indigo (3.7). I get an error when I try to import the file org.springframework.spring-library-3.0.0.RELEASE.libd : Description Resource Path Location Type Archive for required library: 'lib/org.springframework.spring-library-3.0.0.RELEASE.libd' in project 'Spring30' cannot be read or is not a valid ZIP file. One article I read indicated .libd files can not be used with eclipse. But you seemed to have used it? Can you tell me how to get around this? Thanks
Carl
July 12, 2012
Eclipse IDE

Which of the Eclipse IDE should I download to use? There is a wide selection to choose from.
Bryant Castaneda
July 21, 2012
Example

Thank you so much for the example. I am new to the Spring community and this is a great step for newbies.
Alex Zykov
July 22, 2012
In spring 3.1 XmlBeanFactory is deprecated.

You should use DefaultListableBeanFactory. something like this: DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(new GenericXmlApplicationContext( "spring-config.xml")); Spring3HelloWorld myBean = (Spring3HelloWorld) beanFactory .getBean("Spring3HelloWorldBean"); myBean.sayHello();
Anand Roushan
July 27, 2012
spring framework

it's really very informative
Richard Strong
July 27, 2012
Navigation Link

The Previous - Index - Next links are not working. They redisplay the screen you are currently viewing.
sasidhar
July 29, 2012
practice

very useful blog for beginners....thanks lot
Amit Singh
July 29, 2012
Good Work

It worked right on first time.. You guys are just awesome bang on target!! Thanks a lot
Deepa Mani
August 11, 2012
Spring 3

Good Definition. Superb
sultan
August 16, 2012
about hello world spring example

thank you for the example provided here explanation is pretty nice very informative for a beginner : )
gluk
August 17, 2012
example is incomplete

result is Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.<init>(DefaultSingletonBeanRegistry.java:83) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.<init>(FactoryBeanRegistrySupport.java:43) at org.springframework.beans.factory.support.AbstractBeanFactory.<init>(AbstractBeanFactory.java:175) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:157) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:168) at org.springframework.beans.factory.support.DefaultListableBeanFactory.<init>(DefaultListableBeanFactory.java:167) at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:77) at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:66) at net.roseindia.Spring3HelloWorldTest.main(Spring3HelloWorldTest.java:12) Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) ... 9 more what is apache and how to handle it?
Rob Gravelle
August 29, 2012
Awesome!

This is a great tutorial. Easy to follow. Worked the first time. Good stuff! Rob Gravelle
ram
August 31, 2012
Code Is not working

Code is not working dont try with this....it just work
veeracholan
September 9, 2012
Java,Spring

thanks for the tutorial...i 've learnt many concepts out from this site...gud goin dudes...:)
naveen
September 13, 2012
about spring farme work

it is bit difficult understand this framework plz help me how can i become expert in spring framework..
Nagu
September 22, 2012
Problem in main class

Its not working properly :(
vinay
September 27, 2012
spring

i m new to springs i have wrote as it is the code but there is no output in the console so please help me
Sandeep Gupta
November 11, 2012
Sample Spring Applicationm

I have tried to build this sample application using ecliose tools. I have followed all the step but getting NoClassnotfound Exception . Solution: Common Logging jar was missing after setting this jar in class path. sample Application is running perfectly.
Alexander Baggett
November 13, 2012
Deprecated Code used here

XmlBeanFactory is deprecated now. This needs to be updated with a different approach.
Shilpa
November 30, 2012
Unable to extract files

Hi, It is showing as compressed file is invalid/corrupted when we try to open the downloded code. Please make the code readable. -Thankyou Shilpa C S
BACHAN
July 24, 2013
Spring 3 Hello World Example

Thanks very simple and efficient
Tirru
September 10, 2013
Add log jar

add commons-logging jar to run this example
Pramod Nandagudi
November 22, 2013
Starter class missing error

i think ur starter class is missing or u have coded the main method improperly. check for compilation errors before running the code.
Ranjani Murugesan
April 15, 2014
Spring and Hybernate

Its good for all..
DMCA.com