Spring framework 4 source code reading

Source: Internet
Author: User

Spring framework 4 source code reading

I have written several articles about spring, and I feel that it is not very relevant to the topic. Sort out the ideas and write down the article from a more understandable perspective.

The spring skeleton is also the core package of spring. It mainly contains three contents: 1. context: spring online text ------- Director2. core: the core package of spring, mainly including the tools used by spring ------- Item3. beans: spring bean instance ------- Actors


The Director is responsible for arranging the performances, the actors are responsible for performing the performances according to the director's instructions, and props need to be used during the performances.

I think you will understand the general package relationship after reading these images.

Spring Package Structure
You can see the corresponding Package content. The core package focuses on the help class, operation tool, and beans package, and more on the description of bean instances. Context focuses more on global control and function derivation. Next we will continue with a basic summary of the relationship between context and factory to the class:
First, let's look at the beanfactory class under the bean package and the abstract class.
We can see that in the process of generalized implementation of interfaces, each interface inherits some methods of the parent interface while inheriting the parent interface. In this way, we can see that interface-oriented has become a subtle point. BeanFactory [All BeanFactory parent classes]

Some basic methods are defined in beanfactory, including obtaining bean instances by name. HierarchicalBeanFactory [hierarchical BeanFactory]

We can see that this interface implements a hierarchical approach and obtains the LisableBeanFactory parent container of beanFactory.
We can see that the list function is set for beanfactory, and the ability to retrieve the corresponding method from the list is planned. Summary: As you can see from the above class naming and interface planning, beanfactory is constantly abstracted through the continuous inheritance of interfaces. After layer-by-layer subdivision, no class has a single responsibility, and it is more convenient to expand this attribute. For source code, the best way is by name, the most convenient. Context [online/offline]
We can see that the initialization of context is different from that of beanfactory. It can focus on abstract types and specific methods. Most of the methods use the design pattern of the template method, the parent class calls the abstract method, the abstract method is implemented in the subclass, and the object is independent. It is mainly divided into three types: XML, Annotation, and Groovy.
Registry [instance or bean description Register]
Cache the singleton instance in the container for bean registration after initialization. Cache bean description for beanDefinition.
Strategy [initialization policy]
The two initialization policies are simple and cgilib. The mode used here is the policy mode. Context Initialization
/*** Create ClassPathXmlApplicaitonContext under parent, and * read all Bean definitions from XML. * @ param configLocations the configuration file path is c: \ simpleContext. xml * @ param refresh whether to automatically refresh context, refresh --> reload * All bean definitions and create all Singleton instances. * When refresh is true, manually refresh * @ param parent the parent context * @ throws BeansException if context creation failed * @ see # refresh () based on context () */public ClassPathXmlApplicationContext (String [] configLocations, boolean refresh, ApplicationContext parent) throws BeansException {// initialize XmlApplicationContextsuper (parent); // the path of the conversion configuration file setConfigLocations (configLocations ); if (refresh) {// refresh the original context, the key refresh ();}}

Next let's take a look at the AbstractApplicationContext. refresh () method.

// Load or refresh persistent configurations, which may be xml files, properties files, or relational databases. // As a startup method, if initialization fails, the created Singleton will be destroyed to avoid repeated loading of the configuration file. // In other words, after executing this method, do not load the singleton or public void refresh () throws BeansException, IllegalStateException {synchronized (this. startupShutdownMonitor) {// initialize the configuration to prepare for refresh and verify some of the required parameters in the environment variable prepareRefresh (); // inform the inheritance class to destroy the internal factory to create a new factory instance // initialize the Bean instance ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory (); // initialize the basic information of beanFactroy, including classloader, environment, prepareBeanFactory (beanFactory) such as ignored annotations; try {// postProcess in beanfactory can be understood as adding beanFactory to PostProcess in context. postProcessBeanFactory (beanFactory); // execute BeanFactoryPostProcessor (modify the beanFactory parameter before bean initialization during beanfactory initialization), // BeanDefinitionRegistryPostProcessor is actually inherited from BeanFactoryPostProcessor, // BeanDefinitionRegistry support invokeBeanFactoryPostProcessors (beanFactory); // when postProcess is executed, What Is BeanPostProcessor to modify bean content during bean loading, // two methods are used. The Before and After correspond to the registerBeanPostProcessors (beanFactory) Before and After initialization. // MessageSource is initialized, which is mainly used as the initMessageSource () of I18N localization content (); // initialize the event broadcast ApplicationEventMulticaster. Use the observer mode to capture the registered ApplicationEvent time. // initialize the onRefresh () method of the special bean (); // register all ApplicationEventListener to registerListeners () in ApplicationEventMulticaster; // Initialize all beans that are not lazy-init, And the singleton instance finisincluanfactoryinitialization (beanFactory ); // initialize the lifecycle bean and start it (such as the quartz timer). If JMX is enabled, register ApplicationContext to finishRefresh ();} catch (BeansException ex) {// destroy resources. destroyBeans (); // converts the context status to invalid, indicating that the initialization failed flag. cancelRefresh (ex); // transmits the exception to the caller throw ex ;}}}
Starting the above initialization (facade in facade mode) from the time sequence diagram)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.