Design principle of ApplicationContext container

Source: Internet
Author: User

1. In the ApplicationContext container, we use the common filesystemxmlapplicationcontext Implementation as an example to illustrate The design principle of the ApplicationContext container .

2. In Filesystemxmlapplicationcontext design, we see ApplicationContext application context main functions Already in Filesystemxmlapplicationcontext base class abstractxmlapplic ation implemented in filesystemxmlapplicationcontext , as a specific application context , you only need Span style= "color: #ff0000;" > implementation and its own design-related two functions .

3. One feature is that if the app uses filesystemxmlapplicationcontextdirectly, for instance support for instantiating the application context , start the refresh () process for the IOC container. This can be seen in the code implementation of Filesystemxmlapplicationcontext, as follows:

1 /**2 * Create A new filesystemxmlapplicationcontext with the given parent,3 * Loading the definitions from the given XML files.4      * @paramconfiglocations array of file Paths5      * @paramrefresh whether to automatically refresh the context,6 * Loading all beans definitions and creating all singletons.7 * Alternatively, call refresh manually after further configuring the context.8      * @paramParent The parent context9      * @throwsBeansexception If context creation failedTen      * @see#refresh () One      */ A      PublicFilesystemxmlapplicationcontext (string[] configlocations,BooleanRefresh, applicationcontext parent) -             throwsbeansexception { -  the         Super(parent); - setconfiglocations (configlocations); -         if(refresh) { - refresh (); +         } -}
Filesystemxmlapplicationcontext (string[] configlocations,boolean,applicationcontext)

4. This refresh () process involves a series of complex operations initiated by the IOC container , and these operations are similar for different container implementations, so they are encapsulated in the base class. So, what we see in the design of Filesystemxmlapplicationcontext is just a simple call.

5. Another feature is a feature that is specifically related to the filesystemxmlapplicationcontext design, part of the bean definition resource that is associated with how to load XML from the file system about.

6. With this implementation, you can read beandefinition in the file system in XML form, because different application context implementations correspond to different ways of reading beandefinition, The implementation code in Filesystemxmlapplicationcontext is as follows:

1 /**2 * Create A new filesystemxmlapplicationcontext with the given parent,3 * Loading the definitions from the given XML files.4      * @paramconfiglocations array of file Paths5      * @paramrefresh whether to automatically refresh the context,6 * Loading all beans definitions and creating all singletons.7 * Alternatively, call refresh manually after further configuring the context.8      * @paramParent The parent context9      * @throwsBeansexception If context creation failedTen      * @see#refresh () One      */ A      PublicFilesystemxmlapplicationcontext (string[] configlocations,BooleanRefresh, applicationcontext parent) -             throwsbeansexception { -  the         Super(parent); - setconfiglocations (configlocations); -         if(refresh) { - refresh (); +         } -}
Getresourcebypath

As you can see, this method can be used to locate the Filesystemresource resource.

Design principle of ApplicationContext container

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.