Difference in Spring ApplicationContext and BeanFactory, as well as Spring bean scopes

//从ApplicationContext 中取 bean
ApplicationContext ac = new ClassPathXmlApplicationContext ( "com/hsp/beans.xml" ) ;
ac.getBean("beanId");
  When we went to instantiate beans.xml, the configuration file in the bean is instantiated (whether you use or not, bean object at that), and that the object is a singleton singleton. (Each bean has scope attribute can be arbitrarily set as a bean artificial monomorphic, scope = "singleton", scope = "prototype" is obtained for each bean is a new bean).
  Advantages: pre-loaded, when using fast speed.
  Disadvantages: loss of memory, all of the bean are instantiated, but may only use very few of them.
 
//从bean工厂取bea n
BeanFactory factory = new XmlBeanFactory( new ClassPathResource ( "com/hsp/beans.xml" ) ) ;
factory.getBean("beanId");
  If you use a BeanFactory to get bean, when you just instantiate the container, the container of beans will not immediately be instantiated only when you use getBean a bean, will be created in real time.
  Advantages: save memory.
  Cons: Slow.
About Select: Only used in mobile projects in beanfactory, most of the projects (90%) are using ApplicationContext, because they can be loaded in advance, just a waste of memory points.
  One thing to note, the default is singleton singleton with ApplicationContext instantiation xml in the bean. That each bean is only one instance of an object, how many times getBean () method call regardless, no matter how many ref dependence, spring bean container is only one example. But when man set its scope = "prototype" of the time, it is not a single example, but when you load the xml file will not be instantiated (because when you set it to prototype prototype time, spring framework does not You know how many instances you need to create the bean, so it's simply not the instance).
Copy the code
Attach Spring API's interpretation of the scope Spring bean: 
Bean Description: In Spring, those who make up the main body of your application (backbone) and managed by the Spring IoC container object, called bean. Simply put, bean that is, assembly and management of objects, in addition, bean would make no difference by the Spring container initialization and other objects in the application. The bean, and the dependencies between each other bean configuration will be described by the metadata. Bean scopes:   create a bean definition, and its essence is to create a true example of using the bean definition of class "recipe (recipe)". The bean definition of a recipe is important, it is very similar to the class, only you can create multiple instances based on a "prescription." You can control not only the various configuration values and dependent objects can also control the scope of the object. This way you can build the flexibility to choose the scope of the object without having to define the scope of the Java Class level. Spring Framework supports five scopes (which after three attributes can only be used in web-based Spring ApplicationContext, singgleton, prototype can be used in desktop development).   1, singleton: when a scope of the bean is singleton, then there will be only one shared bean instances Spring IoC container, and all requests for the bean, as long as the id matching that bean definition will return the same bean instance.   Note: Singleton scope is the default scope in Spring. To define a singleton in XML bean, may be configured such that:     <bean id = "empServiceImpl" class = "cn.csdn.service.EmpServiceImpl" scope = "singleton">   2, prototype: a plurality of bean definition object instances. Prototype scope bean will result creates a new instance of the bean when (() method which is injected into another bean, or in a container form program calls the getBean) in each of the bean request.
  According to experience, for stateful bean should use the prototype scope, and to stateless bean should use the singleton scope.

 

Guess you like

Origin www.cnblogs.com/yuebuxichen/p/12101293.html