java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot @Component注解

SpringBoot中的@Component注解源码

作者:sunpeiyu

在SpringBoot中,@Component注解用于标识一个类为Bean,并由Spring进行管理,它需要配合@ComponentScan注解使用,后者负责扫描包含@Component的类,并进行自动装配,本文给大家介绍SpringBoot中的@Component注解源码,感兴趣的朋友一起看看吧

@Component注解的作用

@Component注解标识的bean会注入到SpringBoot中,托管给SpringBoot。

使用@Component注解需要注意

@Component需要搭配@ComponentScan注解才可以生效。

@Component注解标识的bean什么时候注入SpringBoot?

我们知道@SpringBootApplication注解,这个组合注解就是使用了@ComponentScan注解,

1. 使用自动装配@ComponentScan注解,入口方法为invokeBeanFactoryPostProcessors();

2. @ComponentScan注解扫描

3. 扫描出来的bean

​​​​​​​Set<BeanDefinitionHolder> scannedBeanDefinitions =
		this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());

4. ComponentScanAnnotationParser.parse();方法

// 解析@ComponentScan注解,扫描注入bean
public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, String declaringClass) {
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this.registry,
			componentScan.getBoolean("useDefaultFilters"), this.environment, this.resourceLoader);
	// bean名称生成器,在解析注册BeanDefinition的时候用到
	Class<? extends BeanNameGenerator> generatorClass = componentScan.getClass("nameGenerator");
	boolean useInheritedGenerator = (BeanNameGenerator.class == generatorClass);
	scanner.setBeanNameGenerator(useInheritedGenerator ? this.beanNameGenerator :
			BeanUtils.instantiateClass(generatorClass));
	// 扫描到@Component组件是是否生成代理以及生成代理方式
	ScopedProxyMode scopedProxyMode = componentScan.getEnum("scopedProxy");
	if (scopedProxyMode != ScopedProxyMode.DEFAULT) {
		scanner.setScopedProxyMode(scopedProxyMode);
	}
	else {
		Class<? extends ScopeMetadataResolver> resolverClass = componentScan.getClass("scopeResolver");
		scanner.setScopeMetadataResolver(BeanUtils.instantiateClass(resolverClass));
	}
	// 扫描路径时规则
	scanner.setResourcePattern(componentScan.getString("resourcePattern"));
	// 如果一个对象如果不加上@Component注解,但是在扫描注解上加上该类的名称,那么也会被扫描加载
	for (AnnotationAttributes includeFilterAttributes : componentScan.getAnnotationArray("includeFilters")) {
		List<TypeFilter> typeFilters = TypeFilterUtils.createTypeFiltersFor(includeFilterAttributes, this.environment,
				this.resourceLoader, this.registry);
		for (TypeFilter typeFilter : typeFilters) {
			scanner.addIncludeFilter(typeFilter);
		}
	}
	// 扫描到某个类时需要忽略它
	for (AnnotationAttributes excludeFilterAttributes : componentScan.getAnnotationArray("excludeFilters")) {
		List<TypeFilter> typeFilters = TypeFilterUtils.createTypeFiltersFor(excludeFilterAttributes, this.environment,
			this.resourceLoader, this.registry);
		for (TypeFilter typeFilter : typeFilters) {
			scanner.addExcludeFilter(typeFilter);
		}
	}
	// 标识扫描注册BeanDefinition后是否延迟初始化,默认false
	boolean lazyInit = componentScan.getBoolean("lazyInit");
	if (lazyInit) {
		scanner.getBeanDefinitionDefaults().setLazyInit(true);
	}
	Set<String> basePackages = new LinkedHashSet<>();
	// 要扫描的路径,如果为空,解析的时候会解析被@ComponentScan标注类的包路径
	String[] basePackagesArray = componentScan.getStringArray("basePackages");
	for (String pkg : basePackagesArray) {
		String[] tokenized = StringUtils.tokenizeToStringArray(this.environment.resolvePlaceholders(pkg),
				ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
		Collections.addAll(basePackages, tokenized);
	}
	// 包路径类,与basePackages互斥
	for (Class<?> clazz : componentScan.getClassArray("basePackageClasses")) {
		basePackages.add(ClassUtils.getPackageName(clazz));
	}
	// 获取启动类所在的包
	if (basePackages.isEmpty()) {
		basePackages.add(ClassUtils.getPackageName(declaringClass));
	}
	scanner.addExcludeFilter(new AbstractTypeHierarchyTraversingFilter(false, false) {
		@Override
		protected boolean matchClassName(String className) {
			return declaringClass.equals(className);
		}
	});
	// 在指定的基本包中执行扫描,返回注册的bean定义。
	return scanner.doScan(StringUtils.toStringArray(basePackages));
}

因为我没有配置@ComponentScan注解中的basePackages属性,所以默认扫描使用启动类所在的包下的bean

​​​​​​​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) {
		// 扫描basePackage包,获取所有配置了@Component注解的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);
				// 注册@Component注解标识的bean,将其放到beanDefinitionMap容器中
				registerBeanDefinition(definitionHolder, this.registry);
			}
		}
	}
	return beanDefinitions;
}

问题1,如何扫描到basePackage包下面的@Component注解的类?

这块的代码逻辑封装在findCandidateComponents(basePackage);方法中

public Set<BeanDefinition> findCandidateComponents(String basePackage) {
	if (this.componentsIndex != null && indexSupportsIncludeFilters()) {
		return addCandidateComponentsFromIndex(this.componentsIndex, basePackage);
	}
	else {
		// 扫描@Component注解的类
		return scanCandidateComponents(basePackage);
	}
}
private Set<BeanDefinition> scanCandidateComponents(String basePackage) {
	// 搜集合适BeanDefinition的集合
	Set<BeanDefinition> candidates = new LinkedHashSet<>();
	try {
		// 包路径 classpath*:com/sunpeiyu/visualweb/**/*.class
		String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
				resolveBasePackage(basePackage) + '/' + this.resourcePattern;
		// 获取包路径下面的所有文件
		Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath);
		boolean traceEnabled = logger.isTraceEnabled();
		boolean debugEnabled = logger.isDebugEnabled();
		// 遍历文件资源
		for (Resource resource : resources) {
			if (traceEnabled) {
				logger.trace("Scanning " + resource);
			}
			try {
				// 读取资源的元信息,本身资源信息resource,元注解信息annotationMetadata
				MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);
				if (isCandidateComponent(metadataReader)) {
					ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
					sbd.setSource(resource);
					// 判断当前资源是否为@Component注解标识的类
					if (isCandidateComponent(sbd)) {
						if (debugEnabled) {
							logger.debug("Identified candidate component class: " + resource);
						}
						// 添加到搜集@Component注解的类的容器
						candidates.add(sbd);
					}
					else {
						if (debugEnabled) {
							logger.debug("Ignored because not a concrete top-level class: " + resource);
						}
					}
				}
				else {
					if (traceEnabled) {
						logger.trace("Ignored because not matching any filter: " + resource);
					}
				}
			}
			catch (FileNotFoundException ex) {
				if (traceEnabled) {
					logger.trace("Ignored non-readable " + resource + ": " + ex.getMessage());
				}
			}
			catch (Throwable ex) {
				throw new BeanDefinitionStoreException(
						"Failed to read candidate component class: " + resource, ex);
			}
		}
	}
	catch (IOException ex) {
		throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);
	}
	return candidates;
}

这块的代码逻辑封装在ClassUtils.getPackageName(declaringClass)方法中,拿到全路径启动类,然后字符串截取前面的包

类似@Component的其他注解@Service、@Controller、@Repository注解

这些注解本身还是@Component注解,所以本身这些注解会走上面一样的方法进行注入bean到beanDefinitionMap中。

@Repository

@Service

@Controller

到此这篇关于SpringBoot中的@Component注解源码的文章就介绍到这了,更多相关SpringBoot @Component注解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文