In this tutorial we are going to see how to create Spring Hello World program.Step by step procedure to create Spring Program using Eclipse given below.
Follow the Simple steps.
1. Select File –> New –> Java Project from your Eclipse IDE.

2. Name your Project. I Created Project Name called Spring-HelloWorld1

3. Add the following jars into your build path.
commons-logging-1.1.3.jar
spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-context-support-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar
4. Now create package com.javatutorialscorner.spring under Spring-HelloWorld1 Project
5. Next create HelloWorld class under com.javatutorialscorner.spring package
HelloWorld.java
01.
package
com.javatutorialscorner.spring;
02.
03.
public
class
HelloWorld {
04.
private
String sayHello;
05.
06.
public
void
getSayHello() {
07.
System.out.println(
"Java Tutorials Corner "
+sayHello);
08.
}
09.
10.
public
void
setSayHello(String sayHello) {
11.
this
.sayHello = sayHello;
12.
}
13.
14.
}
6. Now Create RunApp class which contains Bean configuration path to load configuration from Bean.xml file
RunApp.java
01.
package
com.javatutorialscorner.spring;
02.
03.
import
org.springframework.context.ApplicationContext;
04.
import
org.springframework.context.support.ClassPathXmlApplicationContext;
05.
06.
public
class
RunApp {
07.
08.
/**
09.
* @param args
10.
*/
11.
public
static
void
main(String[] args) {
12.
// TODO Auto-generated method stub
13.
14.
ApplicationContext context =
new
ClassPathXmlApplicationContext(
"Beans.xml"
);
15.
HelloWorld helloWorld = (HelloWorld)context.getBean(
"helloWorld"
);
16.
helloWorld.getSayHello();
17.
}
18.
19.
}
In above program
First step I created Application context using ClassPathXmlApplicationContext which load configuration from bean file which located in class path of application, It take care of creating and initializing all the objects (Beans) declared in bean.xml
Next getBean() method used to get particular bean from created context. This method used bean name as parameter and returns generic object. We can caste to actual object.
Once Object Created you can access any method from that class.
7. Create Bean.xml at where your class files created (ClassPathXmlApplicationContext tries to load bean file from class path).
Bean.xml
01.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02.
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
03.
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
04.
xsi:schemaLocation
=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
>
05.
<
bean
id
=
"helloWorld"
class
=
"com.javatutorialscorner.spring.HelloWorld"
>
06.
<
property
name
=
"sayHello"
value
=
"say Hello"
></
property
>
07.
</
bean
>
08.
09.
</
beans
>
Bean.xml is default name given to bean configuration file. You can choose any name for your bean.xml but you can use file name in main application to create context and your file available in your class path must be same.
Bean.xml is used to assign unique ID to different beans and controls the creation object with different values, using above file you can pass any value to variable sayHello without changing the class file.
Now your First Spring Hello World program ready to run.Now you can run the program see the following output in console.
Java Tutorials Corner say Hello
0 comments:
Post a Comment