The difference between spring entry--applicationcontext and beanfactory

Source: Internet
Author: User

We know how to get the bean from the ApplicationContext container object, but there is another way for the spring framework to get the bean: The Beanfactory code is as follows:

Beanfactory factory = new Xmlbeanfactory (New Classpathresource ("Com/hsp/ioc/beans.xml")); Class 1 Object 1 = (class 1) Factory.getbean ("Class 1");

So what's the difference between the two?

ApplicationContext

When we use ApplicationContext to get the object, as long as we instantiate the container, all the objects are instantiated (provided the bean is configured in singleton mode, and if it is not singleton mode, it is not preloaded because the container cannot determine how many objects the server needs).

    

Beanfactory

And when we use beanfactory to get the bean, we first instantiate the container, whether the object in the container is immediately instantiated and configured according to the configuration file. If the container's bean is configured not to be instantiated immediately, the container will instantiate the object only if the Getbean code is executed to fetch a bean.

Contrast

The advantage of using ApplicationContext is that all objects can be preloaded, the disadvantage is to consume the server's memory, and we use beanfactory, the advantage is to save memory, the disadvantage is that the speed is relatively slow. And there may be errors with null pointer exceptions. And the bean life cycle created by the Bean factory is simpler.

The difference between spring entry--applicationcontext and beanfactory

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.