Spring中的BeanDefinition注册流程详解
前言
NamespaceHandler简单来说就是命名空间处理器,Spring为了开放性提供了NamespaceHandler机制,这样我们就可以根据需求自己来处理我们设置的标签元素。
本文章解析<context:component-scan base-package="xxxxx"/>如何完成bean定义注册的以及扩展点讲解。
BeanDefinition注册流程
测试代码:
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); TestService cs = context.getBean(TestService.class); 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" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- <context:annotation-config /> <aop:aspectj-autoproxy /> --> <context:component-scan base-package="com.study.mike.spring.service"/> <!-- <context:exclude-filter type="annotation" expression=""/> <context:include-filter type="annotation" expression=""/> </context:component-scan> --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:application.properties"/> </bean> <alias name="testService" alias="combat"/> </beans>
流程
先看几个主要的方法:
1、ClassPathXmlApplicationContext构造器
public ClassPathXmlApplicationContext( String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException { //设置父容器 super(parent); //设置配置文件spring.xml setConfigLocations(configLocations); //是否刷新容器 if (refresh) { //执行刷新方法 refresh(); } }
2、AbstractRefreshableApplicationContext#refreshBeanFactory()刷新工厂方法,此方法执行此上下文的基础bean工厂的实际刷新,关闭先前的bean工厂(如果有)并初始化上一个生命周期的下一阶段的新bean工厂。
@Override protected final void refreshBeanFactory() throws BeansException { //如果存在容器就销毁容器 if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { //创建bean容器 DefaultListableBeanFactory beanFactory = createBeanFactory(); //设置序列号id beanFactory.setSerializationId(getId()); customizeBeanFactory(beanFactory); //加载bean定义 loadBeanDefinitions(beanFactory); synchronized (this.beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } }
3、AbstractRefreshableApplicationContext#loadBeanDefinitions(DefaultListableBeanFactory beanFactory),通过XmlBeanDefinitionReader加载bean定义
@Override protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException { // Create a new XmlBeanDefinitionReader for the given BeanFactory. XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); //使用此context的资源加载环境配置bean定义读取器 beanDefinitionReader.setEnvironment(this.getEnvironment()); beanDefinitionReader.setResourceLoader(this); beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this)); //允许子类提供读取器的自定义初始化 initBeanDefinitionReader(beanDefinitionReader); //继续实际加载bean定义 loadBeanDefinitions(beanDefinitionReader); }
4、AbstractXmlApplicationContext#loadBeanDefinitions(XmlBeanDefinitionReader reader),这个方法加载和/或注册bean定义。
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException { //获取到配置文件循环加载 Resource[] configResources = getConfigResources(); if (configResources != null) { reader.loadBeanDefinitions(configResources); } //获取到配置文件(spring.xml)循环加载,这里获取的是ClassPathXmlApplicationContext的构造器 中setConfigLocations(configLocations)设置的文件路径; String[] configLocations = getConfigLocations(); if (configLocations != null) { reader.loadBeanDefinitions(configLocations); } }
5、 XmlBeanDefinitionReader#registerBeanDefinitions(Document doc, Resource resource),注册DOM文档中bean定义。
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException { BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader(); int countBefore = getRegistry().getBeanDefinitionCount(); //createReaderContext(resource) 很关键 documentReader.registerBeanDefinitions(doc, createReaderContext(resource)); return getRegistry().getBeanDefinitionCount() - countBefore; } public XmlReaderContext createReaderContext(Resource resource) { return new XmlReaderContext(resource, this.problemReporter, this.eventListener, this.sourceExtractor, this, getNamespaceHandlerResolver()); } //懒惰地创建一个默认的NamespaceHandlerResolver,如果之前没有设置的话 public NamespaceHandlerResolver getNamespaceHandlerResolver() { if (this.namespaceHandlerResolver == null) { this.namespaceHandlerResolver = createDefaultNamespaceHandlerResolver(); } return this.namespaceHandlerResolver; } protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver() { ClassLoader cl = (getResourceLoader() != null ? getResourceLoader().getClassLoader() : getBeanClassLoader()); //DefaultNamespaceHandlerResolver类中会将加载META-INF/spring.handlers里面配置的:详见下图 return new DefaultNamespaceHandlerResolver(cl);}
红框圈住的都是spring里面自己的扩展实现。例如:
aop对应处理的标签:
<aop:config> <aop:pointcut id="loggerCutpoint" expression= "execution(* com.how2java.service.ProductService.*(..)) "/> <aop:aspect id="logAspect" ref="loggerAspect"> <aop:after pointcut-ref="loggerCutpoint" method="log"/> </aop:aspect> </aop:config>
怎么区分<aop:config/>由AopNamespaceHandler处理呢?接下来就会讲到,parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate)这个方法。
6、DefaultBeanDefinitionDocumentReader#doRegisterBeanDefinitions(Element root),在给定的根<beans />元素中注册每个bean定义。这个方法里面有个关键点!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)启用环境,比如:
我配置这个标签下面的beans是dev环境的,我启用环境是prd,那么dev下的bean是不会被加载的。
protected void doRegisterBeanDefinitions(Element root) { BeanDefinitionParserDelegate parent = this.delegate; this.delegate = createDelegate(getReaderContext(), root, parent); if (this.delegate.isDefaultNamespace(root)) { //获取beans所属环境 String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE); if (StringUtils.hasText(profileSpec)) { //配置为Profile="dev,prd"的转成数组。 String[] specifiedProfiles = StringUtils.tokenizeToStringArray( profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS); //判断当前beans标签的profile和启用环境是否匹配,不匹配直接返回。 if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) { if (logger.isDebugEnabled()) { logger.debug("Skipped XML bean definition file due to specified profiles [" + profileSpec + "] not matching: " + getReaderContext().getResource()); } return; } } } //前置处理,spring没有实现 preProcessXml(root); //正式解析 parseBeanDefinitions(root, this.delegate); //后置处理,spring没有实现 postProcessXml(root); this.delegate = parent; }
7、DefaultBeanDefinitionDocumentReader#parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate),解析文档中根级别的元素。
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; //这里就是判断是由默认的解析器去解析,还是由其他扩展NamespaceHandler处理。 if (delegate.isDefaultNamespace(ele)) { parseDefaultElement(ele, delegate); } else { delegate.parseCustomElement(ele); } } } } else { delegate.parseCustomElement(root); } }
8、BeanDefinitionParserDelegate#parseCustomElement(Element ele, @Nullable BeanDefinition containingBd)
@Nullable public BeanDefinition parseCustomElement(Element ele, @Nullable BeanDefinition containingBd) { String namespaceUri = getNamespaceURI(ele); if (namespaceUri == null) { return null; } //这里的readerContext就是前面我说的很关键的地方,createReaderContext(resource), 接着看resolve(namespaceUri); NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri); if (handler == null) { error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele); return null; } return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd)); }
9、DefaultNamespaceHandlerResolver#resolve(String namespaceUri)。
@Override @Nullable public NamespaceHandler resolve(String namespaceUri) { //获取所有配置到spring.handlers(多个配置文件)里面的命名空间->类, 例:"http://www.springframework.org/schema/aop" -> "org.springframework.aop.config.AopNamespaceHandler" 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); //初始化解析器。 namespaceHandler.init(); //重新put进handlerMappings,方便下次获取时直接是类不是string 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); } } }
10、初始化解析器
public class ContextNamespaceHandler extends NamespaceHandlerSupport { @Override public void init() { registerBeanDefinitionParser("property-placeholder", new PropertyPlaceholderBeanDefinitionParser()); registerBeanDefinitionParser("property-override", new PropertyOverrideBeanDefinitionParser()); registerBeanDefinitionParser("annotation-config", new AnnotationConfigBeanDefinitionParser()); //这个就是解析<context:component-scan base-package="xxxxx"/>标签的解析器 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()); } }
什么时候去执行这个解析呢?
在BeanDefinitionParserDelegate#parseCustomElement(Element ele, @Nullable BeanDefinition containingBd)方法里面的最后一步:
return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
我们看一下这个解析方法是怎么执行的。会调到NamespaceHandlerSupport#parse(Element element, ParserContext parserContext)这个方法.
@Override @Nullable public BeanDefinition parse(Element element, ParserContext parserContext) { BeanDefinitionParser parser = findParserForElement(element, parserContext); //parser.parse(element, parserContext)就会调到ComponentScanBeanDefinitionParser#parse方法 return (parser != null ? parser.parse(element, parserContext) : null); }
@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); ClassPathBeanDefinitionScanner scanner = configureScanner(parserContext, element); //开始扫描,继续看 Set<BeanDefinitionHolder> beanDefinitions = scanner.doScan(basePackages); registerComponents(parserContext.getReaderContext(), beanDefinitions, element); //不知道这么为什么是返回的null,源码就是这样很奇怪。 return null; }
11、ClassPathBeanDefinitionScanner#doScan(String... basePackages),扫描注册。
protected Set<BeanDefinitionHolder> doScan(String... basePackages) { Assert.notEmpty(basePackages, "At least one base package must be specified"); Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>(); for (String basePackage : basePackages) { //扫描到bean定义 Set<BeanDefinition> candidates = findCandidateComponents(basePackage); for (BeanDefinition candidate : candidates) { ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate); candidate.setScope(scopeMetadata.getScopeName()); String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry); if (candidate instanceof AbstractBeanDefinition) { postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName); } if (candidate instanceof AnnotatedBeanDefinition) { AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate); } if (checkCandidate(beanName, candidate)) { BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName); definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry); beanDefinitions.add(definitionHolder); //注册bean定义 registerBeanDefinition(definitionHolder, this.registry); } } } return beanDefinitions; }
12、DefaultListableBeanFactory#registerBeanDefinition(String beanName, BeanDefinition beanDefinition)真正注册bean定义,注册bean定义的工作就此完成。
@Override public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException { Assert.hasText(beanName, "Bean name must not be empty"); Assert.notNull(beanDefinition, "BeanDefinition must not be null"); //判断是否是抽象bean定义,是做其他的处理. if (beanDefinition instanceof AbstractBeanDefinition) { try { ((AbstractBeanDefinition) beanDefinition).validate(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName, "Validation of bean definition failed", ex); } } //beanDefinitionMap就是放bean定义的对象,判断bean定义是否存在. BeanDefinition existingDefinition = this.beanDefinitionMap.get(beanName); if (existingDefinition != null) { if (!isAllowBeanDefinitionOverriding()) { throw new BeanDefinitionOverrideException(beanName, beanDefinition, existingDefinition); } else if (existingDefinition.getRole() < beanDefinition.getRole()) { // e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE if (logger.isInfoEnabled()) { logger.info("Overriding user-defined bean definition for bean '" + beanName + "' with a framework-generated bean definition: replacing [" + existingDefinition + "] with [" + beanDefinition + "]"); } } else if (!beanDefinition.equals(existingDefinition)) { if (logger.isDebugEnabled()) { logger.debug("Overriding bean definition for bean '" + beanName + "' with a different definition: replacing [" + existingDefinition + "] with [" + beanDefinition + "]"); } } else { if (logger.isTraceEnabled()) { logger.trace("Overriding bean definition for bean '" + beanName + "' with an equivalent definition: replacing [" + existingDefinition + "] with [" + beanDefinition + "]"); } } //放入最新的bean定义,注册bean定义的工作就此完成 this.beanDefinitionMap.put(beanName, beanDefinition); } else { if (hasBeanCreationStarted()) { // Cannot modify startup-time collection elements anymore (for stable iteration) synchronized (this.beanDefinitionMap) { this.beanDefinitionMap.put(beanName, beanDefinition); List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1); updatedDefinitions.addAll(this.beanDefinitionNames); updatedDefinitions.add(beanName); this.beanDefinitionNames = updatedDefinitions; if (this.manualSingletonNames.contains(beanName)) { Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames); updatedSingletons.remove(beanName); this.manualSingletonNames = updatedSingletons; } } } else { // Still in startup registration phase this.beanDefinitionMap.put(beanName, beanDefinition); this.beanDefinitionNames.add(beanName); this.manualSingletonNames.remove(beanName); } this.frozenBeanDefinitionNames = null; } if (existingDefinition != null || containsSingleton(beanName)) { resetBeanDefinition(beanName); } }
此次讲了,BeanDefinition注册流程、扩展点NamespaceHandler以及里面的用的策略模式、启用环境相关的,容器刷新AbstractApplicationContext#refresh()的第二步ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
其他的注册流程注解、绝对路径配置文件,都是大同小异。
到此这篇关于Spring中的BeanDefinition注册流程详解的文章就介绍到这了,更多相关BeanDefinition注册流程内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
最新评论