logo

Spring Application


Show

In the Java platform, the Spring Framework acts as an application framework and inversion of control (IoC) container. Here we will provide you with a basic example with a simple step by steps procedure to create a spring application. First of all, just look at the steps below to get a glimpse of how you can create an application in the Spring framework.

  • create the class
  • create the XML file to provide the values
  • create the test class
  • Load the spring jar files
  • Run the test class

Now the detailed description of the above steps are as follows

Create Java Class

First of all, create a simple java bean class that contains only name property.

package com.intellinuts;  

public class Student {  
private String name;  
  
public String getName() {  
    return name;  
}  

public void setName(String name) {  
    this.name = name;  
}  

public void displayInfo(){  
    System.out.println("Hello: "+name);  
}  

}  

As a simple bean class, the above coding consists of only one property name with its getters and setters method. This class contains one extra method named displayInfo() that displays the student name by the hello message.

  1. Create the XML file

In the context of MyEclipse IDE, there is no need to create the XML file as MyEclipse creates this for yourselves. In this Spring Application example, you can create an XML file by opening the applicationContext.xml file and writing the following code:

  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  

<bean id="studentbean" class="com.intellinuts.Student">  
<property name="name" value="Nitin Kaushik"></property>  
</bean>  

</beans>

Here, the bean element defines the bean for the given class. In this example, the property subelement of bean defines the property of the Student class ‘name’. The IOC container will set the value that is specified in the property element of the Student class object.

  1. Create the test class

Now create the test class. Here we are using the getBean() to retrieve the object of the Student class from the IOC container. The coded view of how to perform this is as follows:

package com.javatpoint;  

import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.ClassPathResource;  
import org.springframework.core.io.Resource;  

public class Test {  
public static void main(String[] args) {  
    Resource resource=new ClassPathResource("applicationContext.xml");  
    BeanFactory factory=new XmlBeanFactory(resource);  

    Student student=(Student)factory.getBean("studentbean");  
    student.displayInfo();  
}  
}  

In the above code, The Resource object represents applicationContext.xml file information. Here ClassPathResource is the implementation class of the Resource interface. The BeanFactory is used to return the bean. The implementation class of the BeanFactory is the XmlBeanFactory. In the BeanFactory interface, there are many methods, one of them is getBean(), which returns the object of the associated class.

  1. Load the jar files for the Spring framework

The three main jar files that are required to run the Spring Application are given below:

  • org.springframework.core-3.0.1.RELEASE-A
  • com.springsource.org.apache.commons.logging-1.1.1
  • org.springframework.beans-3.0.1.RELEASE-A

You can download these above Jar files as and when required for your Core Spring Application. Only Spring core jar files are needed to be loaded to run this example.

  1. Run the test class

Finally, run the Test class. The output will be as follows:

Hello: Nitin Kaushik
C:\>cd fsp
C:\fsp> javac -d . Student . java
C:\fsp> java com. intellinuts. Test
Jun 24, 2012 8:11:54 AM org.springframework.beans.fact
Eader loadBeanDefinitions
INFO: loading XML bean definitions from class path res
Xml]
Hello: Nitin Kaushik
C:\fsp>_