What is BeanFactory?
The BeanFactory is the actual container which instantiates, configures, and manages a number of beans.Let have a look at how spring works
How it Works
- When the application is Deployed the Spring framework reads the xml file and creates the objects.Those are the objects which you see in the Spring Container
- Now when you try to refer any of these objects from the outside object using the new method it will throw an exception since or when you try to create a object using new method, the spring container has no idea about the object which you are trying to access
- Now to access the object in the container you will use the BeanFactory Objects
BeanFactory is represented by org.springframework.beans.factory.BeanFactory interface.It is the main and the basic way to access the Spring container.Other ways to access the spring container such as ApplicationContext,ListableBeanFactory, ConfigurableBeanFactory etc. are built upon this BeanFactory interface.
BeanFactory interface defines basic functionality for the Spring Container like
- It is built upon Factory Design Pattern
- provides DI / IOC mechanism for the Spring.
- It loads the beans definitions and their property descriptions from some configuration source (for example, from XML configuration file) .
- Instantiates the beans when they are requested like beanfactory_obj.getBean(“beanId”).
- Wire dependencies and properties for the beans according to their configuration defined in configuration source while instantiating the beans.
- Manage the bean life cycle by bean lifecycle interfaces and calling initialization and destruction methods.
Note that BeanFactory does not create the objects of beans immediately when it loads the configuration for beans from configuration source.Only bean definitions and their property descriptions are loaded. Beans themselves are instantiated and their properties are set only when they are requested such as by getBean() method.
Different BeanFactory Implementations:
XmlBeanFactory using Constructor:
Resource res = new FileSystemResource("c:/beansconfig.xml"); BeanFactory bfObj = new XmlBeanFactory(res); MyBean beanObj= (MyBean) bfObj.getBean("mybean");
- The XmlBeanFactory takes the resource object as Parameter
- bfObj points to the Spring Container from which you try to fetch the object
- mybean is the ID of the Object specified in the XML File
- In the above case BeanFactory loads the beans lazily.BeanFactory will read bean definition of a bean with id “mybean” from beansconfig.xml file, instantiates it and return a reference to that.
- There are tow implementation of Resource Intefrace. one is org.springframework.core.io.FileSystemResource as seen above and other is org.springframework.core.io.ClassPathResource which loads Loads the resource from classpath(shown below).
ClassPathResource resorce = new ClassPathResource ("beansconfig.xml"); BeanFactory factory = new XmlBeanFactory(resource);
ClassPathXmlApplicationContext:
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext( new String[] {"applicationContext.xml", "applicationContext-part2.xml"}); //an ApplicationContext is also a BeanFactory. BeanFactory factory = (BeanFactory) appContext;
Note BeanFactory is not recomended for use in latest Spring versions. It is there only for backward compatability. ApplicationContext is preferred over this because ApplicationContext provides more advance level features which makes an application enterprise level application.