Explanation of IOC, bean and scope of spring (III)
The spring container is the core of the spring framework. The container creates objects, connects them together, configures them, and manages the entire lifecycle from creation to destruction. The spring container uses Di to manage the components that make up the application. These objects are called spring beans.
The container obtains instructions about the objects to be instantiated, configured and assembled by reading the provided configuration metadata. Configuration metadata can be represented by XML, Java annotations, or Java code. The following figure shows a high-level view of how spring works.
1、 IOC
The spring IOC container uses Java POJO classes and configuration metadata to generate fully configured and executable systems or applications
Spring provides the following two different types of containers
The ApplicationContext container includes beanfactory containers for all functions. Therefore, it is generally recommended to use bean factories. Beanfactory can still be used in lightweight applications, such as mobile devices or applet based applications, where data volume and speed are important
II Bean
The objects that form the backbone of the application and are managed by the spring IOC container are called beans. Bean is an object instantiated, assembled and managed by spring IOC container. These beans are created using the configuration metadata you provide to the container. For example, in the form of the XML < bean / > definition you saw in the previous chapter.
The bean definition contains information called configuration metadata. The container needs to know the following-
All of the above configuration metadata is transformed into the following property sets that make up each bean definition.
Spring configuration metadata
The spring IOC container is completely separate from the format in which this configuration metadata is actually written. Here are three important ways to provide configuration metadata for spring containers-
3、 Bean scope
When defining < bean >, you can choose to declare the scope of the bean. For example, to force spring to generate a new bean instance every time it is needed, you should declare the scope attribute of the bean as the prototype. Similarly, if you want spring to return the same bean instance every time you need it, you should declare the scope property of the bean as a singleton.
The spring framework supports the following five scopes, three of which are only available when you use a web enabled ApplicationContext.
(1)SingleTon
This limits the scope of the bean definition to a single instance of each spring IOC container (default).
(2)Prototype
This limits the scope of a single bean definition to any number of object instances.
(3)request
This limits the scope of the bean definition to HTTP requests. Valid only in the context of Web aware spring ApplicationContext.
(4)session
This limits the scope of the bean definition to HTTP sessions. Valid only in the context of Web aware spring ApplicationContext.
(5)gobal session
This limits the scope of the bean definition to global HTTP sessions. Valid only in the context of Web aware spring ApplicationContext.
Example explanation:
Beans. xml
If you do not specify a scope, it is singleton by default. For singleton mode, please refer to my blog post: Singleton mode and JDBC