Yet Another Spring Hello World Application

Yet Another Spring Core Hello World application
Here is another Spring Hello World application which acts as an introductory start to your spring journey. This is very simple and in this post it is discussed with explanation for a better understand.

Folder structure

Project folder: E:\java\spring\springapp2



springapp2
                |_ myconfig.xml
                |_ HelloWorld.java
                |_ HelloWorld.class
                |_ Client1.java
                |_ Client1.class

Spring Configuration - myconfig.xml


<beans xmlns="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-3.0.xsd">

    <bean id="hello" class="HelloWorld">
        <property name="msg" value="Hello World"/>
    </bean>
</beans>

Bean class - HelloWorld.java


class HelloWorld
{
private String msg;
   
    public void setMsg(String msg)
    {
    this.msg=msg;
    }

    public String getMsg()
    {
    return msg;
    }
}

Client1.java


import org.springframework.core.io.*;
import org.springframework.beans.factory.xml.*;
class Client1
{
    public static void main(String args[])
    {
    FileSystemResource f=new FileSystemResource("myconfig.xml");
   
    XmlBeanFactory fact=new XmlBeanFactory(f);
   
    HelloWorld hw=(HelloWorld)fact.getBean("hello");
   
    System.out.println("The message is "+hw.getMsg());
    }
}


Application Output

Remember you need to execute the Client1 and compile all the classes.

May 6, 2013 5:33:09 PM org.springframework.beans.factory.xml.XmlBeanDefinitionRe
ader loadBeanDefinitions
INFO: Loading XML bean definitions from file [E:\java\spring\springapp2\myconfig
.xml]
The message is Hello World

Explaining the code

The myconfig.xml file starts with the <beans> tag and in it, <bean> tag is present.
The <bean> tag takes two attributes one is the id which will be used later to get the object of the class associated with it. The next one is the class attribute which specifies the target bean class.
The <property> tag is another tag which is enclosed in the <bean> tag. This property tag is used to specify the value to be injected to a specified property of the bean class.
Here msg is the property name and the value is Hello World. The msg represents msg member variable in the HelloWorld class and the value to that variable is Hello World.
There is only one property in the bean class.

As you can see, the Bean class contains only one setter and getter methods. The setter method is used to set the value to the msg property and the getter method is to retrieve that value. Value to the msg property is set by the Spring container which reads the xml file.

The Client1.java contains the main() method. Let me explain every statement in the class.

FileSystemResource f=new FileSystemResource("myconfig.xml"): The org.springframework.core.io.FileSystemResource class internally uses the java.io.File. The configuration file is passed to this FileSystemResource class.

XmlBeanFactory fact=new XmlBeanFactory(f): The org.springframework.beans.factory.xml.XmlBeanFactory class takes FileSystemResource object pointing to the spring configuration file. This constructor call will create a light weight Spring Container which parses the XML file using SAXParser and verifies it.

fact.getBean("Hello"); Get the object for the Bean class associated referred by the id Hello. That's it. The object is returned with the value Hello World injected into the member variable msg of the class. As this method returns java.lang.Object type, we need to type cast it in order to access it's methods.

hw.getMsg(): This method present in the class returns the value in the property msg. The value to this property is injected by the Spring Container.

Also see my post on Getting started with Spring Framework. In this way, we can develop a simple hello world application in Spring.For further discussion, feel free to drop a comment.

No comments: