Beanfactory and ApplicationContext

Source: Internet
Author: User

Spring provides two ways to implement a container:

The Bean Factory (Beanfactory interface) provides the underlying dependency injection support;

The application context (ApplicationContext interface), built on the bean Factory basis, provides system architecture services.

At this stage, enterprise development in the choice of more inclined to ApplicationContext interface, because the ApplicationContext interface covers the Beanfactory interface, if using a common Beanfactory interface, Many other features will not work, such as Spring's things transcation and AOP will not work, but why Beanfactory is not abandoned in a way! In some cases, such as running in a resource-constrained device embedded application, memory consumption is very large, hundreds of trillion of memory will also have a great impact, at this time beanfactory is a better choice.

How to get beans using beanfactory (common xmlbeanfactory)

There are several implementations of beanfactory in spring. The most commonly used is org.springframework.beans.factory.xml.XmlBeanFactory, which is loaded in the bean according to the definition in the XML file. To create a xmlbeanfactory, you need to pass an Org.springframework.core.io.Resource instance to the constructor. This resource object provides an XML file to the factory.

There are several ways to configure the XML source:

Org.springframework.core.io.ByteArrayResource

Org.springframework.core.io.ClassPathResource

Org.springframework.core.io.DescriptiveResource

Org.springframework.core.io.FileSystemResource

Org.springframework.core.io.InputStreamResource

For example, use Filesystemresource to create a xmlbeanfactory:

Beanfactory factory = xmlbeanfactory (New Inputstreamresource ("D:\userBean.xml"));

UserBean UserBean = (UserBean) factory.getbean ("UserBean");

Iii. using ApplicationContext to get beans (three common implementations)

On the surface, ApplicationContext and beanfactory are similar. Both load the bean definition information, assemble the bean, and distribute the bean as needed.

However, ApplicationContext provides more features:

1. Provide text information parsing tools, including support for internationalization (i18n).

2. Provides a common method for loading file resources, such as loading pictures.

3. You can send an event to a bean registered as a listener.

Therefore, we should use ApplicationContext more, not beanfactory.

Note: The relationship between the two is that the ApplicationContext interface is extended to Beanfactory

Among the many implementations of ApplicationContext, the following three are frequently used:

Classpathxmlapplicationcontext

Filesystemxmlapplicationcontext

Xmlwebapplicationcontext

The first and second difference is that classpath can look for an XML file that defines the bean in the entire classpath (including the jar file), whereas filesystem can only be found in the specified path.

ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext*.xml");

ApplicationContext context = new Filesystemxmlapplicationcontext ("D:\applicationcontext*.xml");

  

Beanfactory and ApplicationContext

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.