Links to the original text: https://my.oschina.net/u/1590001/blog/268225
For spring configuration file writing, I think that for people who have experienced huge projects, there is that fear, too many configuration files. However, sub module is a method that most people can think of, but how to divide the module into account is that the right person sees the right, and the wise sees wisdom. My strategy is to use import.
The basic code format is as follows
web.xml
<?xml version="1.0" encoding="gb2312"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <import resource="CTIContext.xml" /> <import resource="customerContext.xml" /> <import resource="customerServingContext.xml" /> <import resource="manageContext.xml" /> <import resource="routineContext.xml" /> <import resource="systemContext.xml" /> </beans>
The module configuration file is imported in the application Context. XML file by using import method. If new modules are added in the future, the file can be simply modified, which greatly simplifies the complexity of the configuration file in the later stage.
Spring's parsing code is as follows.
1 /** 2 * Parse an "import" element and load the bean definitions from the given resource 3 * into the bean factory. 4 */ 5 protected void importBeanDefinitionResource(Element ele) { 6 // Obtain resource attribute 7 String location = ele.getAttribute(RESOURCE_ATTRIBUTE); 8 // If it does not exist resource ,Do nothing about it. 9 if (!StringUtils.hasText(location)) { 10 getReaderContext().error("Resource location must not be empty", ele); 11 return; 12 } 13 14 // Resolve system properties: e.g. "${user.dir}" 15 // Resolving System Attributes.Format such as : ${user.dir} 16 location = environment.resolveRequiredPlaceholders(location); 17 18 Set<Resource> actualResources = new LinkedHashSet<Resource>(4); 19 20 // Discover whether the location is an absolute or relative URI 21 // Determine whether the address is absolute or relative 22 boolean absoluteLocation = false; 23 try { 24 absoluteLocation = ResourcePatternUtils.isUrl(location) 25 || ResourceUtils.toURI(location).isAbsolute(); 26 } 27 catch (URISyntaxException ex) { 28 // cannot convert to an URI, considering the location relative 29 // unless it is the well-known Spring prefix "classpath*:" 30 } 31 32 // Absolute or relative? 33 // If it is an absolute address,Load the corresponding configuration file directly according to the address 34 if (absoluteLocation) { 35 try { 36 int importCount = getReaderContext().getReader().loadBeanDefinitions( 37 location, actualResources); 38 if (logger.isDebugEnabled()) { 39 logger.debug("Imported " + importCount 40 + " bean definitions from URL location [" + location + "]"); 41 } 42 } 43 catch (BeanDefinitionStoreException ex) { 44 getReaderContext().error( 45 "Failed to import bean definitions from URL location [" 46 + location + "]", ele, ex); 47 } 48 } 49 else { 50 // No URL -> considering resource location as relative to the current file. 51 // If it is a relative address,Then calculate the absolute address 52 try { 53 int importCount; 54 // Resource Multiple subclasses ,as VfsResource,FileSystemResource,ClassPathResource 55 // And each Resource Of createRelative The methods are different, so I'll try to parse them using subclass methods first., 56 Resource relativeResource = getReaderContext().getResource().createRelative( 57 location); 58 if (relativeResource.exists()) { 59 importCount = getReaderContext().getReader().loadBeanDefinitions( 60 relativeResource); 61 actualResources.add(relativeResource); 62 } 63 else { 64 // If the analysis is unsuccessful,The default parser is used ResourcePatternResolver Parsing 65 String baseLocation = getReaderContext().getResource().getURL().toString(); 66 importCount = getReaderContext().getReader().loadBeanDefinitions( 67 StringUtils.applyRelativePath(baseLocation, location), 68 actualResources); 69 } 70 if (logger.isDebugEnabled()) { 71 logger.debug("Imported " + importCount 72 + " bean definitions from relative location [" + location 73 + "]"); 74 } 75 } 76 catch (IOException ex) { 77 getReaderContext().error("Failed to resolve current resource location", 78 ele, ex); 79 } 80 catch (BeanDefinitionStoreException ex) { 81 getReaderContext().error( 82 "Failed to import bean definitions from relative location [" 83 + location + "]", ele, ex); 84 } 85 } 86 // Listener activation after parsing 87 Resource[] actResArray = actualResources.toArray(new Resource[actualResources.size()]); 88 getReaderContext().fireImportProcessed(location, actResArray, extractSource(ele)); 89 }
Reproduced in: https://my.oschina.net/u/1590001/blog/268225