前言
在上一篇文章中分析了Spring是如何解析預設標籤的,並封裝為BeanDefinition註冊到快取中,這一篇就來看看對於像context這種自定義標籤是如何解析的。同時我們常用的註解如:@Service、@Component、@Controller標註的類也是需要在xml中配置<context:component-scan>才能自動注入到IOC容器中,所以本篇也會重點分析註解解析原理。
正文
自定義標籤解析原理
在上一篇分析預設標籤解析時看到過這個類DefaultBeanDefinitionDocumentReader的方法parseBeanDefinitions:
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這個方法,但在點進去之前不妨想想自定義標籤解析應該怎麼做。
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類中:
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物件並註冊到容器中去?下面就來驗證我們的猜想:
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方法完成掃描註冊了(如果我們要自定義註解是不是也可以這樣實現呢?)。
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,它是做什麼的呢?
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等註解。這裡只是簡單提提,詳細分析留待後篇。
至此,自定義標籤和註解的解析原理就分析完了,下面就看看如何定義我們自己的標籤。
定義我們自己的標籤
通過上面的分析,我相信對於定義自己的標籤流程應該大致清楚了,如下:
- 首先定義一個標籤,並在classpath/META-INF資料夾下建立一個spring.schema檔案,在檔案中指定標籤url和xsd檔案的對應關係(xsd是定義標籤規範的檔案)
- 其次定義其NamespaceHandler類,讓它繼承NamespaceHandlerSupport類;
- 然後定義標籤對應的解析器,並實現parse方法,在parse方法中解析我們的標籤,將其封裝為BeanDefinition物件並註冊到容器中;
- 最後在classpath/META-INF資料夾下建立一個spring.handler檔案,並定義標籤的名稱空間和NamespaceHandler的對映關係。
這就是我們從之前的原始碼分析中理解到的,但這裡實際還忽略了一個步驟,這也是之前分析時沒講到的,你能想到是什麼麼?我們設計的標籤需不需要一個規範?不可能讓其他人隨便寫,否則怎麼識別呢?因此需要一個規範約束。同樣,在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物件,可以看到所有標籤和註解類都會封裝為該物件,因此接下來物件例項化都是根據該物件進行的。