The role and difference between BeanFactory and ApplicationContext in spring?

 

20180508 check

1. BeanFactory class structure:

The BeanFactory interface and its subclasses define the Spring IoC container architecture. Because the BeanFactory system is very large and complex, to understand Spring IoC, it is necessary to clarify the inheritance mechanism of the BeanFactory.

2. The structure of ApplicationContext:

The ApplicationContext interface is a BeanFactory that encapsulates more functions. The most commonly used IoC container in Spring contains two sub-interfaces: ConfigurableApplicationContext and WebApplicationContext.

  1. The container is the core of spring, which enables IoC to manage all and components
  . 2. Two containers of spring: a, BeanFactoy
                                b, ApplicationContext application context
  3, BeanFactory: BeanhFactory uses lazy loading of all beans. In order to get a Bean from BeanhFactory, as long as Call the getBean() method to get the Bean

  4. ApplicationContext: a. Provide text information parsing, support I18N
                         b. Provide general methods for loading file resources
                         c. Send events to beans registered as listeners
                         d. ApplicationContext interface extends BeanFactory interface
                         e. ApplicationContext provides additional functions
  5. ApplicationContext The three implementation classes: a, ClassPathXmlApplication: treat the context file as a class path resource
                                     b, FileSystemXmlApplication: load the context definition information from the XML file in the file system

                                     c. XmlWebApplicationContext: Load context definition information from XML files in the Web system

  6. By default, beans are all monomorphic, and the singleton in <bean> is false
  . 7. The id attribute in <bean> must follow the Java specification, while the name attribute may not follow
  . 8. When instantiating a bean Some initialization actions are required, and when it is no longer used, it needs to be destroyed from the container
  9. Object initialization: <bean init-method="method name">
  10. Object destruction: <bean destroy-method= "Method name">
      The process of destroying the object: a. The main thread is interrupted first.
                            b. The Java virtual machine uses the garbage collection mechanism to recycle
  the bean object. 11. Bean settings: set value injection: 1) Simple configuration: <bean id="xxx" class="Bean's full name class name">
                                                 <property name="xx" value="xxxxx"></property>
                                             </bean>
                                      The value in value can be a basic data type or String type, and spring will automatically determine the setting type and convert it to the appropriate value

                          2) Reference configuration: <bean id="xxx" class="Bean's full name class name">
                                             <property name="xx" ref="xxxxx"></property>
                                         </bean>
                                      The value in ref is the reference data Type, the spring container will complete the acquisition work
                          3) List and array: <bean id="xxx" class="Bean's full name class name">
                                           <property name="list">
                                               <list>
                                                <value></value>
                                                < ref bean=""/>
                                               </list>
                                           </property>
                                        </bean>
                                        The list element can be any element, but cannot violate the object type required by the bean

                          4) Set configuration: same as <list>, change <list> to <set>
                          5) Map configuration: each piece of data in the Map consists of a key and a value, which are defined by the <entry> element

                                     <bean id="xxx" class="Bean的全称类名">
                                           <property name="list">
                                              <entry key="key1">
                                                  <value></value>
                                              </entry>
                                           </property>
                                        </bean>
                                     <bean id="xxx" class="Bean的全称类名">
                                           <property name="list">
                                              <entry key="key1">
                                                  <ref bean=""/>
                                              </entry>
                                           </property>
                                        </bean>
                                Note: When configuring entry, the value of property key can only be String, because Map usually uses String as the primary key

                          6) Properties configuration: using <props> is similar to <map>, the biggest difference is that the value of <prop> is String

                  Note: The use of set value injection must have a set method, and find the set method through the name attribute

                  Advantages: a. It is more intuitive and natural to set through the set() method

                        b. When the dependencies are more complex, it is clearer to use the set() method to
                inject constructors: there must be constructors with no parameters and parameters, plus the index (value starts from 0) attribute to avoid injection confusion

                           <constractor-arg>

               Note: Setter injection can be mixed with constructor injection. First call the constructor to generate the object, and then call the set() method to assign the value. But when using only setter injection, the no-argument constructor will be called first

               Advantages: a. The dependencies are set at one time and are not allowed to be modified externally

                     b. No need to care about the way

                     c. There is a sequence of injections to prevent injection failures

Spring's IoC container is an instantiable class that implements the BeanFactory interface. In fact, Spring provides two different containers: one is the most basic BeanFactory, and the other is the extended ApplicationContext. BeanFactory only provides the most basic dependency injection support, while ApplicationContext extends BeanFactory and provides more additional functions. The initialization of the Bean is also very different between the two. BeanFactory reads the configuration information when it needs to be called, and generates an instance of a class. If the read-in Bean configuration is correct, errors in other configurations will not affect the running of the program. When ApplicationContext initializes, it reads the configuration information of xml into the memory, and checks the XML file. If there is no error in the configuration file, it creates all the beans to serve the application directly. Compared with the basic BeanFactory, the only disadvantage of ApplicationContext is the memory space. When there are many application configuration beans, the program starts slowly.

ApplicationContext will use the Java reflection mechanism to automatically identify BeanPostProcessor, InstantiationAwareBeanPostProcessor and BeanFactoryPostProcessor defined in the configuration file, and automatically register them in the application context; and BeanFactory needs to be registered in the code by manually calling the addBeanPostProcessor() method.

Bean assembly is actually to let the container know which beans are in the program, which can be implemented in the following two ways:
configuration file (the most commonly used, reflecting the idea of ​​dependency injection DI)
programming method (register with BeanFactory during the writing process)

 

effect:

1. BeanFactory is responsible for reading bean configuration documents, managing bean loading, instantiation, maintaining dependencies between beans, and responsible for the declaration cycle of beans.
2. In addition to the functions provided by the above BeanFactory, ApplicationContext also provides more complete framework functions:

a. Internationalization support
b. Resource access: Resource rs = ctx. getResource("classpath:config.properties"), "file:c:/config.properties"
c. Event delivery: by implementing the ApplicationContextAware interface
3. Commonly used acquisitions ApplicationContext methods:
FileSystemXmlApplicationContext: Created from the xml configuration file specified by the file system or url, the parameter is the configuration file name or file name array
ClassPathXmlApplicationContext: Created from the xml configuration file of the classpath, you can read the configuration file from the jar package
WebApplicationContextUtils: from the web The root directory of the application reads the configuration file, which needs to be configured in web.xml first. You can configure a listener or servlet to implement
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
< /listener>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
The default configuration file for both methods is web-inf/applicationContext.xml, or you can use context-param to specify the configuration file
<context-param>
<param-name>contextConfigLocation</param-name>
<param- value>/WEB-INF/myApplicationContext.xml</param-value>
</context-param>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325770868&siteId=291194637