在上一篇文章中分析了Spring是如何解析默認標籤的,並封裝爲BeanDefinition註冊到緩存中,這一篇就來看看對於像context這種自定義標籤是如何解析的。同時咱們經常使用的註解如:@Service、@Component、@Controller標註的類也是須要在xml中配置<context:component-scan>才能自動注入到IOC容器中,因此本篇也會重點分析註解解析原理。java
在上一篇分析默認標籤解析時看到過這個類DefaultBeanDefinitionDocumentReader的方法parseBeanDefinitions:node
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; if (delegate.isDefaultNamespace(ele)) { //默認標籤解析 parseDefaultElement(ele, delegate); } else { //自定義標籤解析 delegate.parseCustomElement(ele); } } } } else { delegate.parseCustomElement(root); } }
如今咱們就來看看parseCustomElement這個方法,但在點進去以前不妨想一想自定義標籤解析應該怎麼作。spring
public BeanDefinition parseCustomElement(Element ele, @Nullable BeanDefinition containingBd) { String namespaceUri = getNamespaceURI(ele); if (namespaceUri == null) { return null; } 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)); }
能夠看到和默認標籤解析是同樣的,只不過由decorate方法改成了parse方法,但具體是如何解析的呢?這裏我就以component-scan標籤的解析爲例,看看註解是如何解析爲BeanDefinition對象的。緩存
進入到parse方法中,首先會進入NamespaceHandlerSupport類中:app
public BeanDefinition parse(Element element, ParserContext parserContext) { BeanDefinitionParser parser = findParserForElement(element, parserContext); return (parser != null ? parser.parse(element, parserContext) : null); }
首先經過findParserForElement方法去找到對應的解析器,而後委託給解析器ComponentScanBeanDefinitionParser解析。在往下看以前,咱們先想想,若是是咱們本身要去實現這個註解解析過程會怎麼作。是否是應該首先經過配置的basePackage屬性,去掃描該路徑下全部的class文件,而後判斷class文件是否符合條件,便是否標註了@Service、@Component、@Controller等註解,若是有,則封裝爲BeanDefinition對象並註冊到容器中去?下面就來驗證咱們的猜測:ide
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對象,用來掃描basePackage包下符合條件(默認是@Component標註的類)的類, // 並建立BeanDefinition類註冊到緩存中 ClassPathBeanDefinitionScanner scanner = configureScanner(parserContext, element); Set<BeanDefinitionHolder> beanDefinitions = scanner.doScan(basePackages); registerComponents(parserContext.getReaderContext(), beanDefinitions, element); return null; }
能夠看到流程和咱們猜測的基本一致,首先建立了一個掃描器ClassPathBeanDefinitionScanner對象,而後經過這個掃描器去掃描classpath下的文件並註冊,最後調用了registerComponents方法,這個方法的做用稍後來說,咱們先來看看掃描器是如何建立的:源碼分析
protected ClassPathBeanDefinitionScanner configureScanner(ParserContext parserContext, Element element) { boolean useDefaultFilters = true; if (element.hasAttribute(USE_DEFAULT_FILTERS_ATTRIBUTE)) { useDefaultFilters = Boolean.valueOf(element.getAttribute(USE_DEFAULT_FILTERS_ATTRIBUTE)); } // Delegate bean definition registration to scanner class. ClassPathBeanDefinitionScanner scanner = createScanner(parserContext.getReaderContext(), useDefaultFilters); scanner.setBeanDefinitionDefaults(parserContext.getDelegate().getBeanDefinitionDefaults()); scanner.setAutowireCandidatePatterns(parserContext.getDelegate().getAutowireCandidatePatterns()); if (element.hasAttribute(RESOURCE_PATTERN_ATTRIBUTE)) { scanner.setResourcePattern(element.getAttribute(RESOURCE_PATTERN_ATTRIBUTE)); } ... parseTypeFilters(element, scanner, parserContext); return scanner; } public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment, @Nullable ResourceLoader resourceLoader) { Assert.notNull(registry, "BeanDefinitionRegistry must not be null"); this.registry = registry; if (useDefaultFilters) { registerDefaultFilters(); } setEnvironment(environment); setResourceLoader(resourceLoader); } protected void registerDefaultFilters() { this.includeFilters.add(new AnnotationTypeFilter(Component.class)); ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader(); try { this.includeFilters.add(new AnnotationTypeFilter( ((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false)); logger.trace("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning"); } catch (ClassNotFoundException ex) { // JSR-250 1.1 API (as included in Java EE 6) not available - simply skip. } try { this.includeFilters.add(new AnnotationTypeFilter( ((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false)); logger.trace("JSR-330 'javax.inject.Named' annotation found and supported for component scanning"); } catch (ClassNotFoundException ex) { // JSR-330 API not available - simply skip. } } protected void parseTypeFilters(Element element, ClassPathBeanDefinitionScanner scanner, ParserContext parserContext) { // Parse exclude and include filter elements. ClassLoader classLoader = scanner.getResourceLoader().getClassLoader(); // 將component-scan的子標籤include-filter和exclude-filter添加到scanner中 NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { String localName = parserContext.getDelegate().getLocalName(node); try { if (INCLUDE_FILTER_ELEMENT.equals(localName)) { TypeFilter typeFilter = createTypeFilter((Element) node, classLoader, parserContext); scanner.addIncludeFilter(typeFilter); } else if (EXCLUDE_FILTER_ELEMENT.equals(localName)) { TypeFilter typeFilter = createTypeFilter((Element) node, classLoader, parserContext); scanner.addExcludeFilter(typeFilter); } } catch (ClassNotFoundException ex) { parserContext.getReaderContext().warning( "Ignoring non-present type filter class: " + ex, parserContext.extractSource(element)); } catch (Exception ex) { parserContext.getReaderContext().error( ex.getMessage(), parserContext.extractSource(element), ex.getCause()); } } } }
上面不重要的方法我已經刪掉了,首先獲取use-default-filters屬性,傳入到ClassPathBeanDefinitionScanner構造器中判斷是否使用默認的過濾器,若是是就調用registerDefaultFilters方法將@Component註解過濾器添加到includeFilters屬性中;建立後緊接着調用了parseTypeFilters方法去解析include-filter和exclude-filter子標籤,並分別添加到includeFilters和excludeFilters標籤中(關於這兩個標籤的做用這裏再也不贅述),因此這一步就是建立包含過濾器的class掃描器,接着就能夠調用scan方法完成掃描註冊了(若是咱們要自定義註解是否是也能夠這樣實現呢?)。post
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) { // 這裏就是實際掃描符合條件的類並封裝爲ScannedGenericBeanDefinition對象 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); } // 解析@Lazy、@Primary、@DependsOn等註解 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); registerBeanDefinition(definitionHolder, this.registry); } } } return beanDefinitions; } public Set<BeanDefinition> findCandidateComponents(String basePackage) { if (this.componentsIndex != null && indexSupportsIncludeFilters()) { return addCandidateComponentsFromIndex(this.componentsIndex, basePackage); } else { // 主要看這,掃描全部符合條件的class文件並封裝爲ScannedGenericBeanDefinition return scanCandidateComponents(basePackage); } } private Set<BeanDefinition> scanCandidateComponents(String basePackage) { Set<BeanDefinition> candidates = new LinkedHashSet<>(); try { String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resolveBasePackage(basePackage) + '/' + this.resourcePattern; // 獲取class文件並加載爲Resource Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath); boolean traceEnabled = logger.isTraceEnabled(); boolean debugEnabled = logger.isDebugEnabled(); for (Resource resource : resources) { if (traceEnabled) { logger.trace("Scanning " + resource); } if (resource.isReadable()) { try { // 獲取SimpleMetadataReader對象,該對象持有AnnotationMetadataReadingVisitor對象 MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource); if (isCandidateComponent(metadataReader)) { // 將AnnotationMetadataReadingVisitor對象設置到ScannedGenericBeanDefinition中 ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader); sbd.setResource(resource); sbd.setSource(resource); if (isCandidateComponent(sbd)) { if (debugEnabled) { logger.debug("Identified candidate component class: " + resource); } candidates.add(sbd); } } } } } } return candidates; }
這個方法實現很複雜,首先是掃描找到符合條件的類並封裝成BeanDefinition對象,接着去設置該對象是否可做爲根據類型自動裝配的標記,而後解析@Lazy、@Primary、@DependsOn等註解,最後纔將其註冊到容器中。
須要注意的是和xml解析不一樣的是在掃描過程當中,建立的是ScannedGenericBeanDefinition對象:
該類是GenericBeanDefinition對象的子類,並持有了AnnotationMetadata對象的引用,進入下面這行代碼:學習
MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);
咱們能夠發現AnnotationMetadata其實是AnnotationMetadataReadingVisitor對象:
從上圖中咱們能夠看到該對象具備不少屬性,基本上包含了咱們類的全部信息,因此後面在對象實例化時須要的信息都是來自於這裏。
以上就是Spring註解的掃描解析過程,如今還剩一個方法registerComponents,它是作什麼的呢?this
protected void registerComponents( XmlReaderContext readerContext, Set<BeanDefinitionHolder> beanDefinitions, Element element) { Object source = readerContext.extractSource(element); CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), source); for (BeanDefinitionHolder beanDefHolder : beanDefinitions) { compositeDef.addNestedComponent(new BeanComponentDefinition(beanDefHolder)); } // Register annotation config processors, if necessary. boolean annotationConfig = true; if (element.hasAttribute(ANNOTATION_CONFIG_ATTRIBUTE)) { annotationConfig = Boolean.valueOf(element.getAttribute(ANNOTATION_CONFIG_ATTRIBUTE)); } if (annotationConfig) { Set<BeanDefinitionHolder> processorDefinitions = AnnotationConfigUtils.registerAnnotationConfigProcessors(readerContext.getRegistry(), source); for (BeanDefinitionHolder processorDefinition : processorDefinitions) { compositeDef.addNestedComponent(new BeanComponentDefinition(processorDefinition)); } } readerContext.fireComponentRegistered(compositeDef); }
在該標籤中有一個屬性annotation-config,該屬性的做用是,當配置爲true時,纔會去註冊一個個BeanPostProcessor類,這個類很是重要,好比:ConfigurationClassPostProcessor支持@Configuration註解,AutowiredAnnotationBeanPostProcessor支持@Autowired註解,CommonAnnotationBeanPostProcessor支持@Resource、@PostConstruct、@PreDestroy等註解。這裏只是簡單提提,詳細分析留待後篇。
至此,自定義標籤和註解的解析原理就分析完了,下面就看看如何定義咱們本身的標籤。
經過上面的分析,我相信對於定義本身的標籤流程應該大體清楚了,以下:
這就是咱們從以前的源碼分析中理解到的,但這裏實際還忽略了一個步驟,這也是以前分析時沒講到的,你能想到是什麼麼?咱們設計的標籤需不須要一個規範?不可能讓其餘人隨便寫,不然怎麼識別呢?所以須要一個規範約束。一樣,在Spring的META-INF文件夾下都會有一個spring.schemas文件,該文件和spring.handler文件同樣,定義了約束文件和約束命名空間的映射關係,下面就是context的spring.schemas文件部份內容:
http\://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context.xsd ...... http\://www.springframework.org/schema/cache/spring-cache.xsd=org/springframework/cache/config/spring-cache.xsd
可是這個文件是在何時被讀取的呢?是否是應該在解析xml以前就把規範設置好?實際上就是在調用XmlBeanDefinitionReader的doLoadDocument方法時讀取的該文件:
protected Document doLoadDocument(InputSource inputSource, Resource resource) throws Exception { return this.documentLoader.loadDocument(inputSource, getEntityResolver(), this.errorHandler, getValidationModeForResource(resource), isNamespaceAware()); } protected EntityResolver getEntityResolver() { if (this.entityResolver == null) { // Determine default EntityResolver to use. ResourceLoader resourceLoader = getResourceLoader(); if (resourceLoader != null) { this.entityResolver = new ResourceEntityResolver(resourceLoader); } else { this.entityResolver = new DelegatingEntityResolver(getBeanClassLoader()); } } return this.entityResolver; } public DelegatingEntityResolver(@Nullable ClassLoader classLoader) { this.dtdResolver = new BeansDtdResolver(); this.schemaResolver = new PluggableSchemaResolver(classLoader); } public static final String DEFAULT_SCHEMA_MAPPINGS_LOCATION = "META-INF/spring.schemas"; public PluggableSchemaResolver(@Nullable ClassLoader classLoader) { this.classLoader = classLoader; this.schemaMappingsLocation = DEFAULT_SCHEMA_MAPPINGS_LOCATION; }
經過兩篇文章完成了對Spring XML標籤和註解解析的源碼分析,總體流程多看幾遍仍是不復雜,關鍵是要學習到其中的設計思想:裝飾、模板、委託、SPI;掌握其中咱們可使用到的擴展點:xml解析先後擴展、自定義標籤擴展、自定義註解擴展(本篇沒有講解,能夠思考一下);深入理解BeanDefinition對象,能夠看到全部標籤和註解類都會封裝爲該對象,所以接下來對象實例化都是根據該對象進行的。