详解Spring如何解析占位符
在以前的Spring Xml配置中我们可能会有如下配置:
<?xml version='1.0' encoding='UTF-8'?><beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:context='http://www.springframework.org/schema/context' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd> <context:property-placeholder ignore-unresolvable='true' location='classpath:jdbc.properties'/> <bean ><property name='url' value='${jdbc.url}'/> </bean></beans>
在上面的配置中jdbc这个Bean的url属性值${jdbc.url}就代表占位符,占位符的真实值就存放在上述配置中的自定义元素的location属性所代表的配置文件jdbc.properties中,这个配置文件里就一行内容:
jdbc.url=127.0.0.1
那问题就来了,Spring又是在什么阶段去解析并且把占位符替换为实际值的呢?
Spring什么时候去解析并占位符从我们就可以知道它是一个自定义xml标签,那Spring势必要解析它,我就直接黏贴Spring解析这个自定义元素的入口代码给各位看官。该代码就在BeanDefinitionParserDelegate这个类中:
/** * Parse the elements at the root level in the document: * 'import', 'alias', 'bean'. * @param root the DOM root element of the document */ //todo doRegisterBeanDefinitions -> parseBeanDefinitions -> parseDefaultElement protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {if (delegate.isDefaultNamespace(root)) { NodeList nl = root.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) {Node node = nl.item(i);if (node instanceof Element) { Element ele = (Element) node; //如果属于beans命名空间 if (delegate.isDefaultNamespace(ele)) {//处理默认标签parseDefaultElement(ele, delegate); } else {//自定义标签//用到了parser//todo parser内部 去注册BeanDefinition 2021-3-15delegate.parseCustomElement(ele); }} }}else { delegate.parseCustomElement(root);} }
主要关注点:delegate.parseCustomElement(ele);
@Nullable public BeanDefinition parseCustomElement(Element ele) {return parseCustomElement(ele, null); } //todo property 子元素 也有可能 解析自定义元素 parsePropertySubElement @Nullable public BeanDefinition parseCustomElement(Element ele, @Nullable BeanDefinition containingBd) {String namespaceUri = getNamespaceURI(ele);if (namespaceUri == null) { return null;}//resolve里有初始化过程//根据命名空间uri获取 NamespaceHandler//todo 获取命名空间下的自定义handler 比如 ContextNamespaceHandlerNamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);if (handler == null) { //todo 如果在spring.handlers配置文件 里没有定义这个命令空间的handler就会 报这个错 2020-09-14 error('Unable to locate Spring NamespaceHandler for XML schema namespace [' + namespaceUri + ']', ele); return null;}//调用parse方法//这里ParserContext注入registry//readerContext里 reader->XmlBeanDefinitionReader 里包含了 registry//TODO 会初始化一个ParserContext进去return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd)); }
到这里我们又要关注这个NamespaceHandler是怎么获取到的,从上面代码可知,Spring会从当前readerContext获取到NamespaceHandlerResolver后通过其resolve方法并根据传入的当前namespaceUri就可以获得当前适合的NamespaceHandler。
/** * Create the {@link XmlReaderContext} to pass over to the document reader. */ public XmlReaderContext createReaderContext(Resource resource) {//把当前reader放进去 ,reader存放了 beanRegistry//beanRegistry 里定义了 registerBeanDefinition 这个重要的方法return new XmlReaderContext(resource, this.problemReporter, this.eventListener,this.sourceExtractor, this, getNamespaceHandlerResolver()); } /** * Lazily create a default NamespaceHandlerResolver, if not set before. * 解析自定义标签时用到 * @see #createDefaultNamespaceHandlerResolver() */ public NamespaceHandlerResolver getNamespaceHandlerResolver() {if (this.namespaceHandlerResolver == null) { this.namespaceHandlerResolver = createDefaultNamespaceHandlerResolver();}return this.namespaceHandlerResolver; } /** * Create the default implementation of {@link NamespaceHandlerResolver} used if none is specified. * <p>The default implementation returns an instance of {@link DefaultNamespaceHandlerResolver}. * @see DefaultNamespaceHandlerResolver#DefaultNamespaceHandlerResolver(ClassLoader) */ protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver() {ClassLoader cl = (getResourceLoader() != null ? getResourceLoader().getClassLoader() : getBeanClassLoader());return new DefaultNamespaceHandlerResolver(cl); } //返回默认的 public DefaultNamespaceHandlerResolver(@Nullable ClassLoader classLoader) {this(classLoader, DEFAULT_HANDLER_MAPPINGS_LOCATION); }
从上面的代码中可知Spring使用的是默认的DefaultNamespaceHandlerResolver,它当然也给开发者留了自定义NamespaceHandlerResolver的机会。那我们现在就可以看看DefaultNamespaceHandlerResolver如何根据namespaceUri解析到对应的NamespaceHandler的。
首先获取到的就是context命名空间,完整路径为http://www.springframework.org/schema/context。我们从DefaultNamespaceHandlerResolver类中可以看到它是如何解析这个命名空间的。
/** * Locate the {@link NamespaceHandler} for the supplied namespace URI * from the configured mappings. * @param namespaceUri the relevant namespace URI * @return the located {@link NamespaceHandler}, or {@code null} if none found */ @Override @Nullable public NamespaceHandler resolve(String namespaceUri) {Map<String, Object> handlerMappings = getHandlerMappings();Object handlerOrClassName = handlerMappings.get(namespaceUri);if (handlerOrClassName == null) { return null;}else if (handlerOrClassName instanceof NamespaceHandler) { return (NamespaceHandler) handlerOrClassName;}else { String className = (String) handlerOrClassName; try {Class<?> handlerClass = ClassUtils.forName(className, this.classLoader);if (!NamespaceHandler.class.isAssignableFrom(handlerClass)) { throw new FatalBeanException('Class [' + className + '] for namespace [' + namespaceUri + '] does not implement the [' + NamespaceHandler.class.getName() + '] interface');}NamespaceHandler namespaceHandler = (NamespaceHandler) BeanUtils.instantiateClass(handlerClass);//todo 命名空间处理器 调用初始化过程 2020-09-04namespaceHandler.init();handlerMappings.put(namespaceUri, namespaceHandler);return namespaceHandler; } catch (ClassNotFoundException ex) {throw new FatalBeanException('Could not find NamespaceHandler class [' + className +'] for namespace [' + namespaceUri + ']', ex); } catch (LinkageError err) {throw new FatalBeanException('Unresolvable class definition for NamespaceHandler class [' +className + '] for namespace [' + namespaceUri + ']', err); }} } /** * Load the specified NamespaceHandler mappings lazily. */ private Map<String, Object> getHandlerMappings() {Map<String, Object> handlerMappings = this.handlerMappings;if (handlerMappings == null) { synchronized (this) {handlerMappings = this.handlerMappings;if (handlerMappings == null) { if (logger.isTraceEnabled()) {logger.trace('Loading NamespaceHandler mappings from [' + this.handlerMappingsLocation + ']'); } try {//todo handlerMappings为空 才去 获取所有属性映射 2020-09-04//spring-aop spring-beans spring-contextProperties mappings =PropertiesLoaderUtils.loadAllProperties(this.handlerMappingsLocation, this.classLoader);if (logger.isTraceEnabled()) { logger.trace('Loaded NamespaceHandler mappings: ' + mappings);}handlerMappings = new ConcurrentHashMap<>(mappings.size());CollectionUtils.mergePropertiesIntoMap(mappings, handlerMappings);this.handlerMappings = handlerMappings; } catch (IOException ex) {throw new IllegalStateException('Unable to load NamespaceHandler mappings from location [' + this.handlerMappingsLocation + ']', ex); }} }}return handlerMappings; }
上面代码中的handlerMappingsLocation一般就是Spring默认的路径:
//指定了默认的handler路径 ,可以传入指定路径改变 /** * The location to look for the mapping files. Can be present in multiple JAR files. */ public static final String DEFAULT_HANDLER_MAPPINGS_LOCATION = 'META-INF/spring.handlers';
我们就回到spring-context项目工程下的resoures/META-INF文件夹下的spring.handlers文件里定义了如下规则:
http://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandlerhttp://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandlerhttp://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandlerhttp://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandlerhttp://www.springframework.org/schema/cache=org.springframework.cache.config.CacheNamespaceHandler
可以看到context自定义命名空间就是对应的ContextNamespaceHandler。我们打开这个类瞧一瞧:
public class ContextNamespaceHandler extends NamespaceHandlerSupport { @Override public void init() {//调用抽象类NamespaceHandlerSupport的注册解析器方法registerBeanDefinitionParser('property-placeholder', new PropertyPlaceholderBeanDefinitionParser());registerBeanDefinitionParser('property-override', new PropertyOverrideBeanDefinitionParser());registerBeanDefinitionParser('annotation-config', new AnnotationConfigBeanDefinitionParser());registerBeanDefinitionParser('component-scan', new ComponentScanBeanDefinitionParser());registerBeanDefinitionParser('load-time-weaver', new LoadTimeWeaverBeanDefinitionParser());registerBeanDefinitionParser('spring-configured', new SpringConfiguredBeanDefinitionParser());registerBeanDefinitionParser('mbean-export', new MBeanExportBeanDefinitionParser());registerBeanDefinitionParser('mbean-server', new MBeanServerBeanDefinitionParser()); }}
发现它只定义了一个init方法,顾名思义就是初始化的意思,那想当然的就是它啥时候会执行初始化呢?我们回到DefaultNamespaceHandler的resolve方法,发现它内部有一处namespaceHandler.init();, 这里就执行了对应命名空间处理器的初始化方法。接下来我们又要看看初始化了做了些啥,我们发现它调用了同一个方法registerBeanDefinitionParser也就是注册
Bean定义解析器,到这里我们先按下暂停键,再捋下上面的整体流程:
Spring在解析自定义标签的时候会根据自定义命名空间去查找合适的NamespaceHandler. 自定义的NamespaceHandler是由NamespaceHandlerResolver去解析得到的。 NamespaceHandlerResolver会根据classLoader.getResources查找所有类路径下的spring.handlers。 查找到后把文件内容转换成handlerMappings,然后根据传入的自定义命名空间匹配到NamespaceHandler 执行NamespaceHandler的init方法注册BeanDefinitionParser。那这个parser做了点什么强大的功能呢?我们下回分解。
以上就是详解Spring如何解析占位符的详细内容,更多关于Spring 解析占位符的资料请关注好吧啦网其它相关文章!
相关文章: