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