<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEbeansPUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd"><beans><!-- In order to create an object of bean we define its properties in bean
tag. The 'id' attribute value can be thought of as a reference to 'class'
attribute value --><beanid="dog"class="com.hubberspot.spring.Dog"/></beans>

3. Create a Test class for the application (imp) -

package com.hubberspot.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class WildLifeApplication {
public static void main(String[] args) {
// BeanFactory is a Spring bean factory which
// provides us with the object requested
// it reads the configuration file and provides
// us with the necessary object required
// We are using XmlBeanFactory because this
// bean factory reads the xml file while preparing
// its objects. We provide XmlBeanfactory with a
// configuration file called as spring.xml placed
// at root of our application. It is passed to first
// FileSystemResource object which provides the
// XmlBeanFactory with the configuration resource
BeanFactory factory = new XmlBeanFactory(
new FileSystemResource("spring.xml"));
// In order to get a object instantiated for a particular
// bean we call getBean() method of BeanFactory passing it the
// id for which the object is to be needed. Here getBean()
// returns an Object. We need to cast it back to the Dog object
// without implementing new keyword we have injected object of
// Dog just by reading an xml configuration file.
Dog dog = (Dog)factory.getBean("dog");
// Calling our functionality
dog.move();
}
}

Output of the program :

Video tutorial to demonstrate how to configure beans in Spring configuration file

A simple example demonstrating the working of BeanFactory using XmlBeanFactory in Spring Framework

<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEbeansPUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd"><beans><!-- In order to create an object of bean we define its properties in bean
tag. The 'id' attribute value can be thought of as a reference to 'class'
attribute value --><beanid="dog"class="com.hubberspot.spring.Dog"/></beans>

3. Create a Test class for the application (imp) -

package com.hubberspot.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class WildLifeApplication {
public static void main(String[] args) {
// BeanFactory is a Spring bean factory which
// provides us with the object requested
// it reads the configuration file and provides
// us with the necessary object required
// We are using XmlBeanFactory because this
// bean factory reads the xml file while preparing
// its objects. We provide XmlBeanfactory with a
// configuration file called as spring.xml placed
// at root of our application. It is passed to first
// FileSystemResource object which provides the
// XmlBeanFactory with the configuration resource
BeanFactory factory = new XmlBeanFactory(
new FileSystemResource("spring.xml"));
// In order to get a object instantiated for a particular
// bean we call getBean() method of BeanFactory passing it the
// id for which the object is to be needed. Here getBean()
// returns an Object. We need to cast it back to the Dog object
// without implementing new keyword we have injected object of
// Dog just by reading an xml configuration file.
Dog dog = (Dog)factory.getBean("dog");
// Calling our functionality
dog.move();
}
}

Output of the program :

Video tutorial to demonstrate how to configure beans in Spring configuration file