This is a basic "Hello World" application using Java Spring. This application uses XML file to configure java Spring into an application. There are other ways as well to configure Spring in to an application.
Before starting an application we need to create an environment to run the Spring on local machine. For that we need Spring libraries and any IDE. For this tutorial I have used Eclipse.
Download Spring libraries from this link. Spring Libraries. I have downloaded 5.0.3.RELEASE/
In addition to this, there is one more library that needs to be downloaded to work using XML file. Commons-logging. Download the first .jar file from this link. We do not need to learn about commons-logging library. It is provided by Apache and Spring uses this library to keep log of running application.
Download and extract all jar files in one folder.
Download and extract all jar files in one folder.
Create Project:
In Eclipse IDE click on File-->New-->Java Project. If you can not find the Java project in new tab click on the other and search for Java Project and create new Java Project. Enter the Project Name "HelloWorld" and click finish.
New Java Project |
![]() |
Add Folder |
Add Spring Libraries:
Right click on Project name, ie. "HelloWorld" and click build path.
Go to Configure build path-->Libraries. Click on Add Library. New popup window will show up.
Select User Library and click next. On next window click on User Libraries. Another window will open with list of user libraries added to the project. Currently it will be empty as there is not any libraries added to the project.
Click on New. Give the name of the library. I gave "SpringJars". Click on Add External Jar and select all jar files from the jars folder of the project. Click on Open.
Add External Jars |
Build Path |
Now, create new package domain under the the src folder. For that right click on src folder-->New-->package. Write the name of the package "domain".
![]() |
Add package |
Now, in domain package create one class HelloWorld.java. And write below code in it.
HelloWorld.java
package domain;
public class HelloWorld {
String firstName;
String lastName;
public void printHello() {
System.out.println("Hello World from " + firstName + " " + lastName);
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Create Main Class:
Create another java class called "HelloWorldMain.java" and copy the code below in it.
public class HelloWorld {
String firstName;
String lastName;
public void printHello() {
System.out.println("Hello World from " + firstName + " " + lastName);
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Create XML File:
Now, Create one XML file called "HelloWorld.xml" in src folder. And add following code to it.
Now, Create one XML file called "HelloWorld.xml" in src folder. And add following code to it.
<?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="HelloWorldBean" class="domain.HelloWorld">
<property name="firstName" value="shashank" />
<property name="lastName" value="patel"></property>
</bean>
</beans>
Here, <beans> tag defines multiple beans in an application. All beans in our application are defined in this tag. For this application we have only one bean.
<bean> tag is for a single bean.
"id" is to provide unique identification to the bean.
"class" is the fully qualified name of the class.
"property" is used to define the properties of the bean class. In our bean class we have two properties: firstName and lastName.
The above XML file declares the Spring bean of "HelloWorldBean" of the class domain.HelloWorld.
Create Main Class:
Create another java class called "HelloWorldMain.java" and copy the code below in it.
package domain;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorldMain {
public static void main(String[] args) {
ApplicationContext beanFactory = new ClassPathXmlApplicationContext("HelloWorld.xml");
HelloWorld myBean = (HelloWorld) beanFactory.getBean("HelloWorldBean");
myBean.printHello();
}
}
In the above code, we have created the instance of ApplicationContext and retrieved "HelloWorldBean". Then we called the printHello() method of the bean.
Run Program:
Right click on a Project. Click on Run as Java Application.
Output:
Hello World from shashank patel
No comments:
Post a Comment