What is a BeanFactory and XMLBeanFactory?

What is a BeanFactory and XMLBeanFactory?

BeanFactory :

- Bean factory is a container. It configures, instantiates and manages a set of beans. These beans are collaborated with one another and have dependencies among themselves. The reflection of these dependencies are used in configuring data that is used by BeanFactory.

XMLBeanFactory :

- XMLBeanFactory is a bean factory that is loaded its beans from an XML file.

What is a BeanFactory and XMLBeanFactory?

- The BeanFactory provides an advanced configuration mechanism capable of managing beans (objects) of any nature, using potentially any kind of storage facility. The BeanFactory is the actual container which instantiates, configures, and manages a number of beans. These beans typically collaborate with one another, and thus have dependencies between themselves. These dependencies are reflected in the configuration data used by the BeanFactory.A BeanFactory is represented by the interface org.springframework.beans.factory.BeanFactory, for which there are multiple implementations.

- Although for most scenarios, almost all user code managed by the BeanFactory does not have to be aware of the BeanFactory, the BeanFactory does have to be instantiated somehow. This can happen via explicit user code such as:
Resource res = new FileSystemResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
ClassPathResource res = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
// of course, an ApplicationContext is just a BeanFactory
BeanFactory factory = (BeanFactory) appContext;
XMLBeanFactory :

- BeanFactory has many implementations in Spring. But one of the most useful one is org.springframework.beans.factory.xml.XmlBeanFactory, which loads its beans based on the definitions contained in an XML file. To create an XmlBeanFactory, pass a java.io.InputStream to the constructor. The InputStream will provide the XML to the factory.

- For example, the following code snippet uses a java.io.FileInputStream to provide a bean definition XML file to XmlBeanFactory.
BeanFactory factory = new XmlBeanFactory(new FileInputStream("beans.xml"));
- To retrieve the bean from a BeanFactory, call the getBean() method by passing the name of the bean you want to retrieve.
MyBean myBean = (MyBean) factory.getBean("myBean");
What is AOP Alliance?
What is AOP Alliance? - AOP Alliance is an open source project. Promoting adoption of AOP and interoperability....
Concepts and purpose of Spring configuration file
Spring configuration file - A spring configuration file is an XML file which contains the information about classes......
What does a simple spring application contain?
What does a simple spring application contain? - A spring application is like any other Java application. The applications contain classes, each of them perform a specific task with the application.......
Post your comment