4.3 spring-嵌入式beans标签的解析
对于嵌入式的beans标签,想信大家很少使用过,或者接触过,起码,我本人就没用过. 它非常类似于Import标签所提供的功能;
使用如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <beans></beans> </beans>
对这个beans没什么太多可讲,解析代码如下:
1 protected void doRegisterBeanDefinitions(Element root) { 2 String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE); 3 // 处理profile属性 4 /* 5 * this is header... 6 * 7 * <beans profile = 'dev"></beans> 8 * 9 * <beans profile = 'production"></beans> 10 * 11 * web.xml 12 * 13 * <context-param> 14 * 15 * <param-name>Spring.profiles.active</param-name> 16 * 17 * <param-value>dev</param-value> 18 * 19 * </context-param> 20 */ 21 if (StringUtils.hasText(profileSpec)) { 22 Assert.state(this.environment != null, 23 "Environment must be set for evaluating profiles"); 24 String[] specifiedProfiles = StringUtils.tokenizeToStringArray(profileSpec, 25 BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS); 26 if (!this.environment.acceptsProfiles(specifiedProfiles)) { 27 return; 28 } 29 } 30 31 // Any nested <beans> elements will cause recursion in this method. In 32 // order to propagate and preserve <beans> default-* attributes correctly, 33 // keep track of the current (parent) delegate, which may be null. Create 34 // the new (child) delegate with a reference to the parent for fallback purposes, 35 // then ultimately reset this.delegate back to its original (parent) reference. 36 // this behavior emulates a stack of delegates without actually necessitating one. 37 // 专门处理解析 38 BeanDefinitionParserDelegate parent = this.delegate; 39 this.delegate = createDelegate(this.readerContext, root, parent); 40 // 解析前留给子类实现 41 preProcessXml(root); 42 43 parseBeanDefinitions(root, this.delegate); 44 // 解析后留给子类实现 45 postProcessXml(root); 46 47 this.delegate = parent; 48 }