A Spring BeanFactory is like a factory class that contains a collection of beans. The Spring BeanFactory holds Bean Definitions of multiple beans within itself and then instantiates the bean whenever asked for by clients.
Popular Tutorials
This is the simplest container providing basic support for DI and defined by the org.springframework.beans.factory.BeanFactory interface. The BeanFactory and related interfaces, such as BeanFactoryAware, InitializingBean, DisposableBean, are still present in Spring for the purposes of backward compatibility with the large number of third-party frameworks that integrate with Spring.
Triangle.java
package com.sdnext.dineshonjava.beanfactory.tutorial; public class Triangle { public void draw() { System.out.println("Drawing Triangle"); } }
spring.xml
<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:> Drawing.javapackage com.sdnext.dineshonjava.beanfactory.tutorial; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.FileSystemResource; /** * @author Dinesh Rajput * */ public class DrawingApp { /** * @param args */ public static void main(String[] args) { //Triangle triangle = new Triangle(); BeanFactory factory = new XmlBeanFactory(new FileSystemResource("spring.xml")); Triangle triangle = (Triangle) factory.getBean("triangle"); triangle.draw(); } }
There are following two important points to note about the main program:
- First step is to create factory object where we used framework API XmlBeanFactory() to create the factory bean and ClassPathResource() API to load the bean configuration file available in CLASSPATH. The XmlBeanFactory() API takes care of creating and initializing all the objects ie. beans mentioned in the configuration file.
- Second step is used to get required bean using getBean() method of the created bean factory object. This method uses bean ID to return a generic object which finally can be casted to actual object. Once you have object, you can use this object to call any class method.
After run the Drawing class we will get following Output on console.Output:Jun 17, 2012 3:47:36 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitionsINFO: Loading XML bean definitions from file [F:my workspacespring03BeanFactoryDemospring.xml]Drawing Triangle
Spring Related Topics you may like
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…