Example of spring application in Myeclipse

Creating spring application in myeclipse IDE is straight forward. You don't should be stressed over the jug records needed for spring application in light of the fact that myeclipse IDE deals with it. We should see the 5 stages to make the first spring application utilizing myeclipse IDE.

  • create the java project
  • add spring capabilities
  • create the class
  • create the xml file to provide the values
  • create the test class

Create the Java Project

Go to File menu - New - venture - Java Project. Compose the undertaking name e.g. firstspring - Finish. Presently the java task is made.

Add spring abilities

Go to Myeclipse menu - Project Capabilities - Add spring abilities - Finish. Presently the spring container documents will be included.

Create Java class

In such case, we are essentially making the Student class have name property. The name of the understudy will be given by the xml document. It is only a straightforward case not the genuine utilization of spring.


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 record

If there should be an occurrence of myeclipse IDE, you don't have to make the xml document as myeclipse does this for yourselves.

		
<?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="studentbean" class="com.javatportal.Student">
<property name="name" value="Niraj Singh"></property>
</bean>

</beans>
		

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 Student class object by the IOC container.

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

The Resource object represents the information of applicationContext.xml file. The Resource is the interface and the ClassPathResource is the implementation class of the Resource interface. The BeanFactory is responsible to return the bean. The XmlBeanFactory is the implementation class of the BeanFactory. There are many methods in the BeanFactory interface. One method is getBean(), which returns the object of the associated class.