在实际开发中,我们经常使用Spring的@Component、@Service、@Repository以及 @Controller等注解来实现bean托管给Spring容器管理。Spring是怎么样实现的呢?我们一起跟着源码看看整个过程吧!

照旧,先看调用时序图:

  1. public AnnotationConfigApplicationContext(String... basePackages) {
  2. this();
  3. scan(basePackages);
  4. refresh();
  5. }

Spring启动时,会去扫描指定包下的文件。

  1. public void scan(String... basePackages) {
  2. Assert.notEmpty(basePackages, "At least one base package must be specified");
  3. this.scanner.scan(basePackages);
  4. }

对应时序图方法1,ClassPathBeanDefinitionScanner#scan。交给ClassPathBeanDefinitionScanner处理。

ClassPathBeanDefinitionScanner 初始化时设置了注解过滤器

  1. public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,Environment environment, @Nullable ResourceLoader resourceLoader) {
  2. Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
  3. this.registry = registry;
  4. if (useDefaultFilters) {
  5. // 注册注解过滤器
  6. registerDefaultFilters();
  7. }
  8. setEnvironment(environment);
  9. setResourceLoader(resourceLoader);
  10. }
  1. protected void registerDefaultFilters() {
  2. // 添加Component类型
  3. this.includeFilters.add(new AnnotationTypeFilter(Component.class));
  4. ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
  5. try {
  6. this.includeFilters.add(new AnnotationTypeFilter(
  7. ((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
  8. }
  9. catch (ClassNotFoundException ex) {
  10. }
  11. try {
  12. this.includeFilters.add(new AnnotationTypeFilter(
  13. ((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));
  14. }
  15. catch (ClassNotFoundException ex) {
  16. }
  17. }

在includeFilters添加了Component,ManagedBean两种注解类型。后面用来过滤加载到的class文件是否需要交给Spring容器管理。

  1. protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
  2. Assert.notEmpty(basePackages, "At least one base package must be specified");
  3. Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
  4. for (String basePackage : basePackages) {
  5. // 扫描包下有Spring Component注解,并且生成BeanDefinition
  6. Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
  7. for (BeanDefinition candidate : candidates) {
  8. // 设置scope,默认是singleton
  9. ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
  10. candidate.setScope(scopeMetadata.getScopeName());
  11. String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
  12. if (candidate instanceof AbstractBeanDefinition) {
  13. postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
  14. }
  15. if (candidate instanceof AnnotatedBeanDefinition) {
  16. AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
  17. }
  18. if (checkCandidate(beanName, candidate)) {
  19. BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
  20. // 生成代理类信息
  21. definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
  22. beanDefinitions.add(definitionHolder);
  23. // 注册到Spring容器
  24. registerBeanDefinition(definitionHolder, this.registry);
  25. }
  26. }
  27. }
  28. return beanDefinitions;
  29. }

对应时序图方法2,ClassPathBeanDefinitionScanner#doScan。该方法对包下class文件解析,符合Spring容器管理的类生成BeanDefinition,并注册到容器中。

扫描包下的class文件,把有Component注解的封装BeanDefinition列表返回。

  1. public Set<BeanDefinition> findCandidateComponents(String basePackage) {
  2. if (this.componentsIndex != null && indexSupportsIncludeFilters()) {
  3. return addCandidateComponentsFromIndex(this.componentsIndex, basePackage);
  4. }
  5. else {
  6. return scanCandidateComponents(basePackage);
  7. }
  8. }

对应时序图方法3,ClassPathScanningCandidateComponentProvider#findCandidateComponents。

  1. private Set<BeanDefinition> scanCandidateComponents(String basePackage) {
  2. Set<BeanDefinition> candidates = new LinkedHashSet<>();
  3. try {
  4. // classpath*:basePackage/**/*.class
  5. String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
  6. resolveBasePackage(basePackage) + '/' + this.resourcePattern;
  7. // 获取 basePackage 包下的 .class 文件资源
  8. Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath);
  9. for (Resource resource : resources) {
  10. // 判断是否可读
  11. if (resource.isReadable()) {
  12. try {
  13. // 获取.class文件类信息
  14. MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);
  15. if (isCandidateComponent(metadataReader)) {
  16. ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
  17. sbd.setResource(resource);
  18. sbd.setSource(resource);
  19. if (isCandidateComponent(sbd)) {
  20. candidates.add(sbd);
  21. }
  22. }
  23. } catch (Throwable ex) {
  24. throw new BeanDefinitionStoreException("Failed to read candidate component class: " + resource, ex);
  25. }
  26. }
  27. }
  28. }
  29. catch (IOException ex) {
  30. throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);
  31. }
  32. return candidates;
  33. }

对应时序图方法4,ClassPathScanningCandidateComponentProvider#scanCandidateComponents。

  1. public MetadataReader getMetadataReader(Resource resource) throws IOException {
  2. if (this.metadataReaderCache instanceof ConcurrentMap) {
  3. // No synchronization necessary...
  4. MetadataReader metadataReader = this.metadataReaderCache.get(resource);
  5. if (metadataReader == null) {
  6. // 获取.class类元信息
  7. metadataReader = super.getMetadataReader(resource);
  8. this.metadataReaderCache.put(resource, metadataReader);
  9. }
  10. return metadataReader;
  11. }
  12. else if (this.metadataReaderCache != null) {
  13. synchronized (this.metadataReaderCache) {
  14. MetadataReader metadataReader = this.metadataReaderCache.get(resource);
  15. if (metadataReader == null) {
  16. metadataReader = super.getMetadataReader(resource);
  17. this.metadataReaderCache.put(resource, metadataReader);
  18. }
  19. return metadataReader;
  20. }
  21. }
  22. else {
  23. return super.getMetadataReader(resource);
  24. }
  25. }

对应时序图方法5,CachingMetadataReaderFactory#getMetadataReader。 super.getMetadataReader(resource) 调用的是 SimpleMetadataReaderFactory#getMetadataReader。

  1. public MetadataReader getMetadataReader(Resource resource) throws IOException {
  2. // 默认是SimpleMetadataReader实例
  3. return new SimpleMetadataReader(resource, this.resourceLoader.getClassLoader());
  4. }
  1. SimpleMetadataReader(Resource resource, @Nullable ClassLoader classLoader) throws IOException {
  2. // 加载.class文件
  3. InputStream is = new BufferedInputStream(resource.getInputStream());
  4. ClassReader classReader;
  5. try {
  6. classReader = new ClassReader(is);
  7. }
  8. catch (IllegalArgumentException ex) {
  9. throw new NestedIOException("ASM ClassReader failed to parse class file - " +
  10. "probably due to a new Java class file version that isn't supported yet: " + resource, ex);
  11. }
  12. finally {
  13. is.close();
  14. }
  15. AnnotationMetadataReadingVisitor visitor = new AnnotationMetadataReadingVisitor(classLoader);
  16. // 解析.class元信息
  17. classReader.accept(visitor, ClassReader.SKIP_DEBUG);
  18. this.annotationMetadata = visitor;
  19. this.classMetadata = visitor;
  20. this.resource = resource;
  21. }

对应时序图方法6,SimpleMetadataReader#SimpleMetadataReader。 组装SimpleMetadataReader。

  1. public void accept(
  2. final ClassVisitor classVisitor,
  3. final Attribute[] attributePrototypes,
  4. final int parsingOptions) {
  5. Context context = new Context();
  6. context.attributePrototypes = attributePrototypes;
  7. context.parsingOptions = parsingOptions;
  8. context.charBuffer = new char[maxStringLength];
  9. ... 省略代码
  10. // Visit the RuntimeVisibleAnnotations attribute.
  11. if (runtimeVisibleAnnotationsOffset != 0) {
  12. int numAnnotations = readUnsignedShort(runtimeVisibleAnnotationsOffset);
  13. int currentAnnotationOffset = runtimeVisibleAnnotationsOffset + 2;
  14. while (numAnnotations-- > 0) {
  15. // Parse the type_index field.
  16. String annotationDescriptor = readUTF8(currentAnnotationOffset, charBuffer);
  17. currentAnnotationOffset += 2;
  18. // 这里面封装Spring Component注解
  19. currentAnnotationOffset =
  20. readElementValues(classVisitor.visitAnnotation(annotationDescriptor,true),
  21. currentAnnotationOffset,true,charBuffer);
  22. }
  23. }
  24. ... 省略代码
  25. }

对应时序图方法7,ClassReader#accept。该方法把二进制的.class文件解析组装到AnnotationMetadataReadingVisitor

  1. private int readElementValues(
  2. final AnnotationVisitor annotationVisitor,
  3. final int annotationOffset,
  4. final boolean named,
  5. final char[] charBuffer) {
  6. ... 省略代码
  7. if (annotationVisitor != null) {
  8. // 主要逻辑还在这里面
  9. annotationVisitor.visitEnd();
  10. }
  11. return currentOffset;
  12. }

对应时序图方法8,ClassReader#readElementValues。

  1. public void visitEnd() {
  2. super.visitEnd();
  3. Class<? extends Annotation> annotationClass = this.attributes.annotationType();
  4. if (annotationClass != null) {
  5. ... 省略代码
  6. // 过滤java.lang.annotation包下的注解,及保留Spring注解
  7. if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotationClass.getName())) {
  8. try {
  9. // 获取该类上的所有注解
  10. Annotation[] metaAnnotations = annotationClass.getAnnotations();
  11. if (!ObjectUtils.isEmpty(metaAnnotations)) {
  12. Set<Annotation> visited = new LinkedHashSet<>();
  13. for (Annotation metaAnnotation : metaAnnotations) {
  14. // 过滤java.lang.annotation包下的注解,及保留Spring注解
  15. recursivelyCollectMetaAnnotations(visited, metaAnnotation);
  16. }
  17. // 封装需要的注解
  18. if (!visited.isEmpty()) {
  19. Set<String> metaAnnotationTypeNames = new LinkedHashSet<>(visited.size());
  20. for (Annotation ann : visited) {
  21. metaAnnotationTypeNames.add(ann.annotationType().getName());
  22. }
  23. this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames);
  24. }
  25. }
  26. }
  27. catch (Throwable ex) {
  28. }
  29. }
  30. }
  31. }

对应时序图方法9,AnnotationAttributesReadingVisitor#visitEnd。过滤掉 java.lang.annotation 包下的注解,然后把剩下的注解放到metaAnnotationMap。

  1. protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
  2. for (TypeFilter tf : this.excludeFilters) {
  3. if (tf.match(metadataReader, getMetadataReaderFactory())) {
  4. return false;
  5. }
  6. }
  7. for (TypeFilter tf : this.includeFilters) {
  8. if (tf.match(metadataReader, getMetadataReaderFactory())) {
  9. return isConditionMatch(metadataReader);
  10. }
  11. }
  12. return false;
  13. }

对应时序图方法10,ClassPathScanningCandidateComponentProvider#isCandidateComponent。使用前面提过的ClassPathBeanDefinitionScanner初始化时设置的注解类型过滤器,includeFilters 包含ManagedBean和Component类型。

  1. public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
  2. throws IOException {
  3. if (matchSelf(metadataReader)) {
  4. return true;
  5. }
  6. ... 省略代码
  7. return false;
  8. }

对应时序图方法11,AbstractTypeHierarchyTraversingFilter#match。

  1. protected boolean matchSelf(MetadataReader metadataReader) {
  2. AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
  3. return metadata.hasAnnotation(this.annotationType.getName()) ||
  4. (this.considerMetaAnnotations && metadata.hasMetaAnnotation(this.annotationType.getName()));
  5. }

对应时序图方法12,AnnotationTypeFilter#matchSelf。判断类的metadata中是否包含Component。

总结@Component到Spring bean容器管理过程。第一步,初始化时设置了Component类型过滤器;第二步,根据指定扫描包扫描.class文件,生成Resource对象;第三步、解析.class文件并注解归类,生成MetadataReader对象;第四步、使用第一步的注解过滤器过滤出有@Component类;第五步、生成BeanDefinition对象;第六步、把BeanDefinition注册到Spring容器。以上是@Component注解原理,@Service、@Controller和@Repository上都有@Component修饰,所以原理是一样的。

Spring注解Component原理源码解析的更多相关文章

  1. Spring Boot中@ConfigurationProperties注解实现原理源码解析

    0. 开源项目推荐 Pepper Metrics是我与同事开发的一个开源工具(https://github.com/zrbcool/pepper-metrics),其通过收集jedis/mybatis ...

  2. 【Spring实战】Spring注解配置工作原理源码解析

    一.背景知识 在[Spring实战]Spring容器初始化完成后执行初始化数据方法一文中说要分析其实现原理,于是就从源码中寻找答案,看源码容易跑偏,因此应当有个主线,或者带着问题.目标去看,这样才能最 ...

  3. 【转】【Spring实战】Spring注解配置工作原理源码解析

    一.背景知识 在[Spring实战]Spring容器初始化完成后执行初始化数据方法一文中说要分析其实现原理,于是就从源码中寻找答案,看源码容易跑偏,因此应当有个主线,或者带着问题.目标去看,这样才能最 ...

  4. vue双向绑定原理源码解析

    当我们学习angular或者vue的时候,其双向绑定为我们开发带来了诸多便捷,今天我们就来分析一下vue双向绑定的原理. 简易vue源码地址:https://github.com/maxlove123 ...

  5. 设计模式课程 设计模式精讲 8-8 单例设计模式-Enum枚举单例、原理源码解析以及反编译实战

    1 课堂解析 2 代码演练 2.1 枚举类单例解决序列化破坏demo 2.2 枚举类单例解决序列化破坏原理 2.3 枚举类单例解决反射攻击demo 2.4 枚举类单例解决反射攻击原理 3 jad的使用 ...

  6. 从ReentrantLock详解AQS原理源码解析

    数据结构 java.util.concurrent.locks.AbstractQueuedSynchronizer类中存在如下数据结构. // 链表结点 static final class Nod ...

  7. java基础(十八)----- java动态代理原理源码解析

    关于Java中的动态代理,我们首先需要了解的是一种常用的设计模式--代理模式,而对于代理,根据创建代理类的时间点,又可以分为静态代理和动态代理. 静态代理 1.静态代理 静态代理:由程序员创建或特定工 ...

  8. Spring Boot自动装配原理源码分析

    1.环境准备 使用IDEA Spring Initializr快速创建一个Spring Boot项目 添加一个Controller类 @RestController public class Hell ...

  9. Spring 注解@Component,@Service,@Controller,@Repository

    Spring 注解@Component,@Service,@Controller,@RepositorySpring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释, ...

随机推荐

  1. 理解urllib、urllib2及requests区别及运用

    urllib and urllib2 区别 –博主提示:下面的是python2中的用法,python3需要做出相应修改. urllib和urllib2模块都做与请求URL相关的操作,但他们提供不同的功 ...

  2. 线程的interrupt()

    官网解释 If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) method ...

  3. Java中final、finally、finalize

    简述 final 可以用来修饰类.方法.变量,分别有不同的意义: final 修饰的 class 代表不可以继承扩展: final 的变量是不可以修改的:final 的方法也是不可以重写的(overr ...

  4. 【洛谷2053】 [SCOI2007]修车(费用流)

    传送门 洛谷 Solution 考虑把每一个修车工人拆成\(n\)个点,那么考虑令\(id(i,j)\)为第\(i\)个工人倒数第\(j\)次修车. 然后就可以直接跑费用流了!!! 代码实现 /* m ...

  5. shell练习1

    题目 把ls -l 的输出按照属主分类,打印每个属住的文件名 ls -l |sed -n '2,$p'| awk ' {hash[$]=hash[$]} END{ for (user in hash) ...

  6. C#调用新浪微博API

    WebRequest wq = WebRequest.Create(this.address); HttpWebRequest hq = wq as HttpWebRequest; string us ...

  7. centos7下面改变亮度

    通过改自己的配置文件来改变亮度的方法在我的机器上面是不中用了,我的是台式机,我用的集成显卡,目前我找到的还勉强的方法就是通过安装redshift来改变屏幕的亮度,我就是不喜欢太亮,差点瞎了. 具体是这 ...

  8. linux下如何单独编译设备树?

    答: make <vendor>/<device_name>.dtb 如: make freescale/fsl-1043a-rdb.dtb

  9. LC 969. Pancake Sorting

    Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...

  10. LuceneUtil使用反射,封装通用的方法

    package loaderman.util; import java.io.File; import java.lang.reflect.Method; import loaderman.entit ...