Background
[wp_ad_camp_1]
XmlBeanFactory is one of Spring’s containers that provide dependency injection. Note that it has been deprecated already. This means that you should not use this in new development projects.
Software Environment
- Windows 7 Professional SP1
- Eclipse – Kepler Release
- Java 1.7 (1.7.0_67 – Windows x86)
- Spring Framework
- Spring Context 4.1.2
Maven Dependencies
1 2 3 4 5 6 7 | <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.2.RELEASE</version> </dependency> </dependencies> |
Bean Configuration File
A bean configration file is an XML file that contains all objects instantiated when the container starts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="mike" class="com.turreta.spring.ioc.bean.PersonBean"> <property name="lastName" value="Fox"/> <property name="firstName" value="Michael"/> <property name="middleName" value="J"/> </bean> <bean id="rock" class="com.turreta.spring.ioc.bean.PersonBean"> <property name="lastName" value="Johnson"/> <property name="firstName" value="Dwayne"/> <property name="middleName" value="Douglas"/> </bean> <bean id="ant" class="com.turreta.spring.ioc.bean.AnimalBean"> <property name="name" value="Ant"/> <property name="description" value="Small inserts"/> </bean> <bean id="kangaroo" class="com.turreta.spring.ioc.bean.AnimalBean"> <property name="name" value="Kangaroo"/> <property name="description" value="They kick real hard"/> </bean> </beans> |
JavaBeans
AnimalBean.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.turreta.spring.ioc.bean; public class AnimalBean { private String name; private String description; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String toString() { return this.name + " = " + this.description; } } |
PersonBean.java
[wp_ad_camp_5]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package com.turreta.spring.ioc.bean; public class PersonBean { private String lastName; private String firstName; private String middleName; public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getMiddleName() { return middleName; } public void setMiddleName(String middleName) { this.middleName = middleName; } public String toString() { return this.firstName + " " + this.middleName + " " + this.lastName; } } |
The Main Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import com.turreta.spring.ioc.bean.AnimalBean; import com.turreta.spring.ioc.bean.PersonBean; public class MainClass { @SuppressWarnings("deprecation") public static void main(String[] args) { XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource( "allbeans.xml")); PersonBean mike = (PersonBean) factory.getBean("mike"); System.out.println(mike.toString()); PersonBean rock = (PersonBean) factory.getBean("rock"); System.out.println(rock.toString()); AnimalBean ant = (AnimalBean) factory.getBean("ant"); System.out.println(ant.toString()); AnimalBean kangaroo = (AnimalBean) factory.getBean("kangaroo"); System.out.println(kangaroo.toString()); } } |
Sample Output
1 2 3 4 5 6 | Nov 24, 2014 10:55:37 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [allbeans.xml] Michael J Fox Dwayne Douglas Johnson Ant = Small inserts Kangaroo = They kick real hard |
Download the Project
[wp_ad_camp_4]
https://www.dropbox.com/s/tfcxwdj2x49jx1i/turretaSpringDIBeanFactory.zip?dl=0