spring context:component-scan标签实现原理

基于spring3.2.16版本说明

通常情况下我们在创建spring项目的时候,在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的class文件,如果扫描到文件中带有@Service,@Component,@Repository,@Controller等这些注解的类,则把这些类注册为bean

         注:如果配置了那么标签就可以不用在xml中再配置了,因为前者包含了后者。另外还提供了两个子标签


配置文件如下,主要说明context:component-scan的实现原理。



 

 











application/json;charset=utf-8





解析配置文件时,不同的标签,会采用不同的解析类解析。代码实现在DefaultBeanDefinitionDocumentReader类parseBeanDefinitions方法内,protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate)。对应标签,采用ComponentScanBeanDefinitionParser类进行解析。

解析的过程,可以参看下面的流程图。目前实在不知道如何在csdn中调节图片大小,可以下载在本地放大查看。核心思想,就是获取对应的路径下,所有的class文件,构建成resource数组,然后迭代这个数组,对每个文件进行注解类型判断。如果class中有注解,则将注册对应的bean。注解的判断,借用到了asm框架,可以参看博客中有关asm的介绍。http://blog.csdn.net/gaoshan12345678910/article/details/78131784


spring context:component-scan标签实现原理_第1张图片



有关ClassReader解析字节码的问题,属于asm问题,在博客中会有单独介绍。这里,只需要知道
classReader.accept(visitor, ClassReader.SKIP_DEBUG);代码会将class内对应的注解信息赋值给visitor即可
/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.springframework.core.type.classreading;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.asm.ClassReader;
import org.springframework.core.NestedIOException;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;


/**
 * {@link MetadataReader} implementation based on an ASM
 * {@link org.springframework.asm.ClassReader}.
 *
 * 

Package-visible in order to allow for repackaging the ASM library  * without effect on users of the {@code core.type} package.  *  * @author Juergen Hoeller  * @author Costin Leau  * @since 2.5  */ final class SimpleMetadataReader implements MetadataReader { private final Resource resource; private final ClassMetadata classMetadata; private final AnnotationMetadata annotationMetadata; SimpleMetadataReader(Resource resource, ClassLoader classLoader) throws IOException { InputStream is = new BufferedInputStream(resource.getInputStream()); ClassReader classReader; try { classReader = new ClassReader(is); } catch (IllegalArgumentException ex) { throw new NestedIOException("ASM ClassReader failed to parse class file - " + "probably due to a new Java class file version that isn't supported yet: " + resource, ex); } finally { is.close(); } AnnotationMetadataReadingVisitor visitor = new AnnotationMetadataReadingVisitor(classLoader); //classReader.accept(visitor, ClassReader.SKIP_DEBUG);执行前,visitor.getAnnotationTypes() = [] //调用reader的接受方法,这个方法实际就是解析class字节码的实现,这里使用了Visitor模式 classReader.accept(visitor, ClassReader.SKIP_DEBUG); //classReader.accept(visitor, ClassReader.SKIP_DEBUG);执行后,visitor.getAnnotationTypes() = [org.springframework.stereotype.Controller] this.annotationMetadata = visitor; // (since AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisitor) this.classMetadata = visitor; this.resource = resource; } public Resource getResource() { return this.resource; } public ClassMetadata getClassMetadata() { return this.classMetadata; } public AnnotationMetadata getAnnotationMetadata() { return this.annotationMetadata; } }

/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.springframework.core.type.classreading;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.asm.ClassReader;
import org.springframework.core.NestedIOException;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;


/**
 * {@link MetadataReader} implementation based on an ASM
 * {@link org.springframework.asm.ClassReader}.
 *
 *

Package-visible in order to allow for repackaging the ASM library
 * without effect on users of the {@code core.type} package.
 *
 * @author Juergen Hoeller
 * @author Costin Leau
 * @since 2.5
 */
final class SimpleMetadataReader implements MetadataReader {

private final Resource resource;

private final ClassMetadata classMetadata;

private final AnnotationMetadata annotationMetadata;

SimpleMetadataReader(Resource resource, ClassLoader classLoader) throws IOException {
InputStream is = new BufferedInputStream(resource.getInputStream());
ClassReader classReader;
try {
classReader = new ClassReader(is);
}
catch (IllegalArgumentException ex) {
throw new NestedIOException("ASM ClassReader failed to parse class file - " +
"probably due to a new Java class file version that isn't supported yet: " + resource, ex);
}
finally {
is.close();
}
AnnotationMetadataReadingVisitor visitor = new AnnotationMetadataReadingVisitor(classLoader);
//classReader.accept(visitor, ClassReader.SKIP_DEBUG);执行前,visitor.getAnnotationTypes() = []
//调用reader的接受方法,这个方法实际就是解析class字节码的实现,这里使用了Visitor模式
classReader.accept(visitor, ClassReader.SKIP_DEBUG);
//classReader.accept(visitor, ClassReader.SKIP_DEBUG);执行后,visitor.getAnnotationTypes() = [org.springframework.stereotype.Controller]
this.annotationMetadata = visitor;
// (since AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisitor)
this.classMetadata = visitor;
this.resource = resource;
}
public Resource getResource() {
return this.resource;
}
public ClassMetadata getClassMetadata() {
return this.classMetadata;
}
public AnnotationMetadata getAnnotationMetadata() {
return this.annotationMetadata;
}
}

你可能感兴趣的:(spring)