Steps to create spring Application

Here, we are going to take in the basic strides to make the first spring application. To run this application, we are not utilizing any IDE. We are essentially utilizing the order brief.

Steps to create spring application

Let's see the 5 steps to create the first spring application.

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

Create Java class

This is the straight forward java bean class containing the name property just. This is straightforward bean class, containing one and only property name with its getters and setters technique. This class contains one additional technique named displayInfo() that prints the under study name by the welcome message.

package com.javatportal;

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);
}
}

 

Create the xml document

If there should be an occurrence of myeclipse IDE, you don't have to make the xml record as myeclipse does this for yourselves. The bean component is utilized to characterize the bean for the given class. The property sub element of bean determines the property of the Student class named name. The quality indicated in the property component will be set in the Student class object by the IOC holder.

		
<?xml version="1.0" encoding="UTF-8"?>
<bean>
	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.javatportal.Student">
<property name="name" value="Mithi"></property>
</bean>

</beans>

Create the test class

Make the java class e.g. Test. Here we are getting the object of Student class from the IOC holder utilizing the getBean() system for BeanFactory.

package com.javatportal;

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();
}
}

Load the container records needed for spring system

There are primarily three jug records needed to run this application.

  • 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

Run the test class

Presently run the Test class.