How to develop a Spring Application

Now i will explain how to develop a spring application...Dear frnds this is very very important for fresher. Lets see.

To develop a spring application is very simple. So Let's see the following steps to develop a spring application by using eclipse IDE.

But to run this example, you need to load only spring core jar files.

To load the jar files in eclipse IDE, Right click on your project - Build Path - Add external archives - select all the required jar files - finish..

  • First the configuration xml file of spring is loaded into Resource object. Resource is an interface. Technically the loading of xml file is called bootstrapping. Then ClassPathResource is an implementation class which is implemented from Resource interface. Both are present in "org.springframework.core.io.*";

    The general form is:

    Resource resource=new ClassPathResource(“configuration xml file”); Basically this xml file contains the details of bean about our application.

  • Then we will take IOC container such as BeanFactory(an interface) which is created by configuration xml by through Resource object. BeanFactory is responsible for creating the bean objects and for injecting the dependencies throughout the program. XmlBeanFactory is an implementation class of it. The general form is:

    BeanFactory factory=new XmlBeanFactory(resource);

  • Then we will get object from BeanFactory by invoking getBean() method, we have to pass the bean id as parameter and this method always returns object of class Object. Here we need to type cast into our bean type. The general form is:

    Object obj=factory.getBean("id1");
    DemoBean db=(DemoBean)obj;

  • Go to File menu - New - project - Java Project. Write the project name e.g. springdemo - Finish. Now the java project is created.
  • There are some jar files required to run this application, such as:-
  • 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
  • Then we will create a java class. in such case, we are simply creating the Student class have name property. The name of the student will be provided by the xml file. It is just a simple example not the actual use of spring. We will see the actual use in Dependency Injection chapter. To create the java class, Right click on src - New - class - Write the class name e.g. Employee - finish. Then the following code is:-

package java8s;

public class Employee {
    private String ename;
    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public void show() {
        System.out.println("Hiii: " + ename);
    }
}

This is simple bean class, containing only one property name with its getters and setters method. This class contains one extra method named show() that displays the employee name.

Then we will create the xml file:-

To create the xml file click on src - new - file - give the file name such as applicationContext.xml - finish. Open the applicationContext.xml file, and write the following code:-

Example

<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: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="id1" class="java8s.Employee">
<property name="ename" value="Divyasundar Sahu"> </property>
</bean>
</beans>
</xml>

The bean element is used to define the bean for the given class. The property subelement of bean specifies the property of the Student class named name. The value specified in the property element will be set in the Employee class object by the IOC container.

Then we will create the test class

package com.javarace;
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 SpringDemo {
    public static void main(String[] args) {
        Resource resource = new ClassPathResource("applicationContext.xml");
        BeanFactory bf = new XmlBeanFactory(resource);
        Employee e = (Employee) bf.getBean("Employee");
        e.show();
    }
}

Create the java class e.g. Test. Here we are getting the object of Student class from the IOC container using the getBean() method of BeanFactory. Let's see the code of test class.

Output

Hi Divyasundar Sahu

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 0674-2361252

Comment