springsteps
springsteps
•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
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..
3) Create 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.
Student - finish. Write the following code:
package com.javatpoint;
xsi:schemaLocation="http://www.springframework.org/schem
a/beans
http://www.springframework.org/schema/beans/spring-beans-
3.0.xsd">
</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.
5) Create the test class
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.
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;
Student student=(Student)factory.getBean("studentbean");
student.displayInfo();
}
}