Spring命名空间

context:component-scan

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
	<context:component-scan base-package="bean" use-default-filters="false">
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</beans>

该元素包含两部分信息:元素前缀context以及local元素component-scan。因为bean是默认的命名空间,所以不需要有前缀。根据schema规范,元素前缀需要显示的关联命名空间namespace,如上面的xmlns:context,而命名空间又需要与其处理类建立映射关系,其配置在相对于classpath的规约资源/META-INF/spring.handlers文件中,如:
在这里插入图片描述在这里插入图片描述
显然context对应的处理器为ContextNamespaceHandler。
Spring容器根据/META-INF/spring.handlers的配置,定位到命名空间,然后定位到处理器ContextNamespaceHandler。当Spring上下文启动时,调用
init() 方法:
在这里插入图片描述
将该命名空间下的所有local元素的Bean定义解析器,通过registerBeanDefinitionParser注册。component-scan的Bean定义解析器为ComponentScanBeanDefinitionParser
在XML Schema校验的过程中,需要定位命名空间所对应的schema文件,这部分声明由xsi:schemaLocation属性关联,如本文开头代码所示:http://www.springframework.org/schema/context 对应于http://www.springframework.org/schema/context/spring-context-4.2.xsd
其配置在/META-INF/spring.schema文件中,并且根据映射找到org/springframework/context/config/spring-context.xsd约束文件
在这里插入图片描述
在这里插入图片描述
如身上图的 xsd:element name=“component-scan” 就是对应于component-scan的约束

ComponentScanBeanDefinitionParser解析器

当spring上下文加载并解析xml配置文件,所有的local元素被注册后,当解析至Component-Scan元素时,parse()方法被调用

public class ComponentScanBeanDefinitionParser implements BeanDefinitionParser {

	private static final String BASE_PACKAGE_ATTRIBUTE = "base-package";
	
	@Override
	@Nullable
	public BeanDefinition parse(Element element, ParserContext parserContext) {
		String basePackage = element.getAttribute(BASE_PACKAGE_ATTRIBUTE);
		basePackage = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(basePackage);
		String[] basePackages = StringUtils.tokenizeToStringArray(basePackage,
				ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);

		// Actually scan for bean definitions and register them.
		ClassPathBeanDefinitionScanner scanner = configureScanner(parserContext, element);
		Set<BeanDefinitionHolder> beanDefinitions = scanner.doScan(basePackages);
		registerComponents(parserContext.getReaderContext(), beanDefinitions, element);

		return null;
	}
}

注意,如果是基于@ComponentScan注解的,则器解析器为org.springframework.context.annotation.ComponentScanAnnotationParser

以管窥豹,其他的命名空间也是这样的原理

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值