In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What is the principle of Spring core container IOC? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
1. BeanFactory
The creation of Spring Bean is a typical factory pattern, and this series of Bean factories, that is, IOC containers, provide a lot of convenience and basic services for developers to manage dependencies between objects. The most basic IOC container interface BeanFactory, take a look at its source code:
Public interface BeanFactory {/ / A pair of escaped definitions of FactoryBean, because if the object retrieved by FactoryBean using the name of bean is a factory-generated object, / / if you need to get the factory itself, you need to escape String FACTORY_BEAN_PREFIX = "&"; / / according to the name of bean, get the bean instance in the IOC container Object getBean (String name) throws BeansException;// to get the bean instance according to the name and Class type of bean, adding a type safety verification mechanism. T getBean (String name, @ Nullable Class requiredType) throws BeansException;Object getBean (String name, Object...) Args) throws BeansException; T getBean (Class requiredType) throws BeansException; T getBean (Class requiredType, Object...) Args) throws BeansException;// provides retrieval of bean to see if there is a beanboolean containsBean (String name) with this name in the IOC container; / / get the bean instance based on the bean name, and determine whether the bean is a singleton boolean isSingleton (String name) throwsNoSuchBeanDefinitionException;boolean isPrototype (String name) throwsNoSuchBeanDefinitionException;boolean isTypeMatch (String name, ResolvableType typeToMatch) throwsNoSuchBeanDefinitionException;boolean isTypeMatch (String name, @ NullableClass typeToMatch) throwsNoSuchBeanDefinitionException;// to get the Class type @ NullableClass getType (String name) throwsNoSuchBeanDefinitionException of bean instance / / get the alias of bean. If retrieved according to the alias, the original name will also be retrieved from String [] getAliases (String name);}
II. BeanDefinition
The SpringIOC container manages the various Bean objects we define and their relationships, and Bean objects are described as BeanDefinition in the Spring implementation.
III. BeanDefinitionReader
The parsing of Bean is mainly the parsing of Spring configuration files. This parsing process is mainly done through BeanDefintionReader.
Fourth, the first experience of Web IOC container
After the IOC container is initialized, the onRefresh () method of DispatcherServlet is finally called, and in the onRefresh () method, the initStrategies () method is called directly to initialize the nine components of SpringMVC:
/ * * This implementation calls {@ link # initStrategies}. * / @ Overrideprotected void onRefresh (ApplicationContext context) {initStrategies (context);} / * Initialize the strategy objects that this servlet uses.*
May be overridden in subclasses in order to initialize further strategy objects.*/// initialization policy protected void initStrategies (ApplicationContext context) {/ / component initMultipartResolver (context) for multiple file uploads; / / initialize local locale initLocaleResolver (context); / / initialize template processor initThemeResolver (context); / / handlerMappinginitHandlerMappings (context); / / initialize parameter adapter initHandlerAdapters (context); / / initialize exception interceptor initHandlerExceptionResolvers (context); / / initialize view preprocessor initRequestToViewNameTranslator (context) / / initialize the view converter initViewResolvers (context); / / initFlashMapManager (context);}
Summary of initialization of IOC container:
Now, using the above code, summarize the basic steps for initializing the IOC container:
1. The initialized entry is done by the refresh call in the container.
two。 The method used to load the IOC container for the Bean definition is loadBeanDefinition.
This is the answer to the question about the principle of Spring core container IOC. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.