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