BeanFactory Spring Example
August 16, 2013
BeanFactory is an interface and belongs to org.springframework.beans.factory package. BeanFactory is the container which manages beans, initializes and handles their dependencies. In the container, beans will interact with each other. BeanFactory has many implementations and one of them is org.springframework.beans.factory.xml.XmlBeanFactory.
XmlBeanFactory in Spring
XmlBeanFactory has been deprecated in Spring 3.1. XmlBeanFactory is the implementation ofBeanFactory
. XmlBeanFactory can be initialized with the help of ClassPathResource
and FileSystemResource
.
BeanFactory with ClassPathResource
ClassPathResource
belongs to the org.springframework.core.io package. ClassPathResource acts as java.io.File in system. XmlBeanFactory is initialized by ClassPathResource as below.
BeanFactoryWithClassPathResource.java
package com.concretepage; 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 BeanFactoryWithClassPathResource { public static void main(String... args) { Resource res = new ClassPathResource("spring-app.xml"); BeanFactory factory = new XmlBeanFactory(res); Entitlement ent = (Entitlement)factory.getBean("entitlement"); System.out.println(ent.getName()); } }
spring-app.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <bean id="entitlement" class="com.concretepage.Entitlement"> <constructor-arg name="name" value="Hello! My name is Ram"/> <constructor-arg name="time" value="20"/> </bean> </beans>
Entitlement.java
package com.concretepage; public class Entitlement { private String name; private int time; public Entitlement(String name,int time){ this.name=name; this.time=time; } public String getName() { return name; } public int getTime() { return time; } }
build.gradle
apply plugin: 'java' apply plugin: 'eclipse' archivesBaseName = 'SpringAOP' version = '1' repositories { mavenCentral() } dependencies { compile 'org.springframework.boot:spring-boot-starter:1.2.7.RELEASE' }
Output
Hello! My name is Ram
BeanFactory with FileSystemResource
FileSystemResource belongs to the org.springframework.core.io package. FileSystemResource acts as java.io.File and also as supports resolution for URL. XmlBeanFactory is initialized by FileSystemResource as below.BeanFactoryWithFileSystemResource.java
package com.concretepage; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; public class BeanFactoryWithFileSystemResource { public static void main(String... args) { Resource res = new FileSystemResource("src/main/resources/spring-app.xml"); BeanFactory factory = new XmlBeanFactory(res); Entitlement ent = (Entitlement)factory.getBean("entitlement"); System.out.println(ent.getName()); } }
BeanFactory with ApplicationContext
BeanFactory can also be obtained by type casting ApplicationContext. Find the example below.SpringDemo.java
package com.concretepage; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringDemo { public static void main(String... args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-app.xml"); BeanFactory factory = (BeanFactory) context; Entitlement ent = (Entitlement)factory.getBean("entitlement"); System.out.println(ent.getName()); } }