The difference between spring's beanfactory and ApplicationContext

Source: Internet
Author: User

To load a bean from an XML configuration file using Beanfactory:

Import Org.springframework.beans.factory.xml.XmlBeanFactory;

Import Org.springframework.core.io.FileSystemResource;

public class Xmlconfigwithbeanfactory {

public static void Main (string[] args) {

Xmlbeanfactory factory = new Xmlbeanfactory (New Filesystemresource (

"Build/beans.xml"));

}

}

To load a bean from an XML configuration file using ApplicationContext:

public class xmlconfigwithapplication{

public static void Main (string[] args) {

ApplicationContext application = new Classpathxmlapplicationcontext (beans.xml "));

Application.getbean ("Beanname");

}

}

Both are loaded Bean,applicationcontext and beanfacotry by the XML configuration file, providing more extensibility, but the main difference is that the latter is lazy loading, if one of the bean's properties is not injected, Beanfacotry is loaded, the exception is thrown until the first use of the call Getbean method, while the ApplicationContext initializes itself as a test, which helps to check whether the dependent property is injected ; so we usually choose to use ApplicationContext.

1). Beanfactroy uses lazy loading as a way to inject beans, that is, only when a bean is used (called Getbean ()), the bean is loaded instantiated, so that we cannot discover some of the existing spring configuration problems. ApplicationContext, on the other hand, creates all the beans at once when the container is started. This way, when the container starts, we can discover the configuration errors that exist in spring.
2). Beanfactory and ApplicationContext both support the use of beanpostprocessor, beanfactorypostprocessor, but the difference between the two is: Beanfactory need to register manually, And ApplicationContext is automatically registered

------------------------------------------------------  

Beanfacotry is the more primitive factory in spring. such as Xmlbeanfactory is a typical beanfactory. The original beanfactory cannot support many of spring's plugins, such as AOP features, Web applications, and so on.
The ApplicationContext interface, which is derived from the Beanfactory interface, provides all beanfactory functionality. ApplicationContext works in a more framework-oriented manner and hierarchies and implements inheritance for contexts, the ApplicationContext package also provides the following features:
Messagesource, providing international message access
• Resource access, such as URLs and files
• Event Propagation
• Load Multiple (inherited relationships) contexts so that each context is focused on a specific level, such as the Web tier of the application
1. Using Messagesource for internationalization
Beanfactory is not supported for internationalization because Beanfactory does not extend the Messageresource interface in spring. In contrast, since ApplicationContext expands the Messageresource interface, it has the ability to process messages (i18n), and the specifics of how spring uses internationalization are described in detail later in the chapter.

2. Powerful event mechanism
essentially, the design of the event involves the observer pattern. Do not understand the observer pattern of friends, it is best to learn about the Internet. Because, this mode in Java development is more commonly used, it is more important.
The ApplicationContext event mechanism is provided primarily through the two interfaces of Applicationevent and Applicationlistener, just like the event mechanism in Java swing. That is, when an event is published in ApplicationContext, all beans that extend the Applicationlistener will receive the event and handle it accordingly.

Spring provides some of the built-in events, mainly in the following ways:
when Contextrefreshedevent:applicationcontext sends this event, it means that all the beans in the container have been loaded and that the ApplicationContext is ready for use
contextstartedevent: Start signal for life cycle beans
contextstoppedevent: Stop signal for life cycle beans
Contextclosedevent:applicationcontext Shutdown event, the context cannot be refreshed and restarted, so all singleton beans are destroyed (because the singleton bean exists in the container cache)

Although spring provides many built-in events, users can also extend things in spriong to their own needs.  Note that the events to be extended are implemented with the Applicationevent interface.

3. Access to the underlying resources
ApplicationContext extends the Resourceloader (resource loader) interface, which can be used to load multiple resource, while Beanfactory is not extended Resourceloader

4. Support for Web applications
Unlike beanfactory, which is usually created programmatically, ApplicationContext can be created declaratively, such as using Contextloader. Of course you can also use one of the ApplicationContext implementations to programmatically create a ApplicationContext instance.  

Contextloader has two implementations:Contextloaderlistener and contextloaderservlet. These two have the same functionality, except that listener cannot be used in servlet 2.2-compatible containers. Since the Servelt 2.4 specification, listener has been asked to initialize after the Web application is started. Many 2.3 compatible containers have implemented this feature. Which one you use depends on yourself, but if all the conditions are the same, you will probably prefer Contextloaderlistener; For more information on compatibility, refer to contextloaderservlet The Javadoc.

This listener needs to check the contextconfiglocation parameter. If it does not exist, it will use /web-inf/applicationcontext.xmlby default. If it exists , it separates the strings with a pre-defined delimiter (comma, semicolon, and space) and places the values as the application context will be searched. Contextloaderservlet can be used to replace Contextloaderlistener. This servlet uses the contextconfiglocation parameter like listener.

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.