网站建设与管理实施方案,在什么网站上可以做免费广告,在百度上做网站有用吗,成都市住房和建设局官网本文主要讲述Spring是如何解析“context:component-scan”元素#xff0c;扫描加载目录下的BeanDefinition。
解析内容
1、解析的元素如下#xff1a; !-- 注解模式#xff1a;配置bean扫描路径#xff08;注#xff1a;自动包含子路径#xff09; --conte…本文主要讲述Spring是如何解析“context:component-scan”元素扫描加载目录下的BeanDefinition。
解析内容
1、解析的元素如下 !-- 注解模式配置bean扫描路径注自动包含子路径 --context:component-scan base-packagecom.learnsf.main,com.learnsf.service/注该元素解析过程中会自动处理“context:annotation-config/”元素要解析的内容。 2、只扫描加载目录下的BeanDefinition不对注解进行解析。在AbstractApplicationContext.invokeBeanFactoryPostProcessors统一解析。
解析
ComponentScanBeanDefinitionParser.parse(Element element, ParserContext parserContext)
解析元素“context:component-scan”。 OverrideNullablepublic BeanDefinition parse(Element element, ParserContext parserContext) {// 获取属性“base-package”多个包路径String basePackage element.getAttribute(BASE_PACKAGE_ATTRIBUTE);// 对包路径中占位符进行替换处理basePackage parserContext.getReaderContext().getEnvironment().resolvePlaceholders(basePackage);// 分解成包数组String[] basePackages StringUtils.tokenizeToStringArray(basePackage,ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);// 生成BeanDefinition扫描器ClassPathBeanDefinitionScanner scanner configureScanner(parserContext, element);// 扫描生成beanDefinitionSetBeanDefinitionHolder beanDefinitions scanner.doScan(basePackages);// 注册BeanDefinitionregisterComponents(parserContext.getReaderContext(), beanDefinitions, element);return null;}ClassPathBeanDefinitionScanner.doScan(String… basePackages)
在包里扫描BeanDefinition。 protected SetBeanDefinitionHolder doScan(String... basePackages) {Assert.notEmpty(basePackages, At least one base package must be specified);SetBeanDefinitionHolder beanDefinitions new LinkedHashSet();// 每个包进行扫描for (String basePackage : basePackages) {// 获取候选BeanDefinitionSetBeanDefinition candidates findCandidateComponents(basePackage);// 每个侯选者处理for (BeanDefinition candidate : candidates) {// 解析Bean作用域ScopeMetadata scopeMetadata this.scopeMetadataResolver.resolveScopeMetadata(candidate);candidate.setScope(scopeMetadata.getScopeName());// 生成beanName String beanName this.beanNameGenerator.generateBeanName(candidate, this.registry);if (candidate instanceof AbstractBeanDefinition abstractBeanDefinition) {// 对 AbstractBeanDefinition类型的BeanDefinition 进一步处理赋值BeanDefinition属性默认值并设置 autowireCandidate 属性postProcessBeanDefinition(abstractBeanDefinition, beanName);}if (candidate instanceof AnnotatedBeanDefinition annotatedBeanDefinition) {// 对 AnnotatedBeanDefinition 类型的 BeanDefinition 进一步处理对通用注解的解析处理通用注解包括 Lazy、Primary、DependsOn、Role、Description。例如如果当前类被Lazy修饰则会获取Lazy 的value 值并保存到 BeanDefinition#lazyInit 属性中。AnnotationConfigUtils.processCommonDefinitionAnnotations(annotatedBeanDefinition);}// 检查给定候选bean的beanName确定相应的bean定义是否需要注册或与现有定义冲突if (checkCandidate(beanName, candidate)) {// 封装候选BeanDefinition为BeanDefinitionHolder BeanDefinitionHolder definitionHolder new BeanDefinitionHolder(candidate, beanName);// 对 BeanDefinitionHolder 填充代理信息definitionHolder AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);// 加入到返回集合beanDefinitions.add(definitionHolder);// 注册BeanDefinitionHolder 到bean工厂容器中registerBeanDefinition(definitionHolder, this.registry);}}}return beanDefinitions;}ClassPathScanningCandidateComponentProvider.findCandidateComponents(String basePackage)
ClassPathScanningCandidateComponentProvider是ClassPathBeanDefinitionScanner父类。 获取注解的Bean的BeanDefinition。 public SetBeanDefinition findCandidateComponents(String basePackage) {if (this.componentsIndex ! null indexSupportsIncludeFilters()) {// Indexed 注解的处理 注1return addCandidateComponentsFromIndex(this.componentsIndex, basePackage);}else {// 非Indexed 注解的处理return scanCandidateComponents(basePackage);}}注1Indexed注解Spring在5.0版本引入的主要解决启动时注解模式解析太长的问题。处理方式是在项目编译打包时会自动生成META-INF/spring.components文件文件包含被Indexed注释的类的模式解析结果。当Spring应用上下文进行组件扫描时META-INF/spring.components会被org.springframework.context.index.CandidateComponentsIndexLoader读取并加载转换为CandidateComponentsIndex对象此时组件扫描会读取CandidateComponentsIndex而不进行实际扫描从而提高组件扫描效率减少应用启动时间。如果使用该功能需要引入如下依赖
dependencygroupIdorg.springframework/groupIdartifactIdspring-context-indexer/artifactIdversion${spring.version}/versionoptionaltrue/optional
/dependencyClassPathScanningCandidateComponentProvider.scanCandidateComponents(String basePackage)
该方法是从指定的包路径获取到字节码文件筛选出可能注入到Spring容器的Bean生成对应的ScannedGenericBeanDefinition private SetBeanDefinition scanCandidateComponents(String basePackage) {SetBeanDefinition candidates new LinkedHashSet();try {// // 形成完整包路径String packageSearchPath ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX resolveBasePackage(basePackage) / this.resourcePattern;// 扫描路径下的资源字节码文件Resource[] resources getResourcePatternResolver().getResources(packageSearchPath);boolean traceEnabled logger.isTraceEnabled();boolean debugEnabled logger.isDebugEnabled();// 遍历所有资源字节码文件挑选有注解的字节码文件生成BeanDefinitionfor (Resource resource : resources) {String filename resource.getFilename();if (filename ! null filename.contains(ClassUtils.CGLIB_CLASS_SEPARATOR)) {// Ignore CGLIB-generated classes in the classpathcontinue;}if (traceEnabled) {logger.trace(Scanning resource);}try {// 获得资源的MetadataReader包含文件信息和对应类注解信息MetadataReader metadataReader getMetadataReaderFactory().getMetadataReader(resource);// 校验是否是候选组件条件是包含在include-filters(扫描时需要实例化的类默认都包含且 Conditional注解中不跳过的类默认都不跳过if (isCandidateComponent(metadataReader)) {ScannedGenericBeanDefinition sbd new ScannedGenericBeanDefinition(metadataReader);sbd.setSource(resource);// 校验是否是候选组件bean是独立且具体的类 或者 是抽象类但被Lookup注解修饰if (isCandidateComponent(sbd)) {if (debugEnabled) {logger.debug(Identified candidate component class: resource);}// 加入返回的候选组件集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;}ComponentScanBeanDefinitionParser.registerComponents( XmlReaderContext readerContext, Set beanDefinitions, Element element) protected void registerComponents(XmlReaderContext readerContext, SetBeanDefinitionHolder beanDefinitions, Element element) {Object source readerContext.extractSource(element);// 构建CompositeComponentDefinitionCompositeComponentDefinition compositeDef new CompositeComponentDefinition(element.getTagName(), source);// 将所有BeanDefinition添加到compositeDef的nestedComponents属性中for (BeanDefinitionHolder beanDefHolder : beanDefinitions) {compositeDef.addNestedComponent(new BeanComponentDefinition(beanDefHolder));}// Register annotation config processors, if necessary.// 处理“annotation-config”假定annotation-config是存在这意味着配置了“context:component-scan”则不需要再配置“context:annotation-config”boolean annotationConfig true;if (element.hasAttribute(ANNOTATION_CONFIG_ATTRIBUTE)) {// 获取component-scan标签的annotation-config属性值默认为trueannotationConfig Boolean.parseBoolean(element.getAttribute(ANNOTATION_CONFIG_ATTRIBUTE));}if (annotationConfig) {// 获取所有处理注解类的BeanPostProcessors(BeanPostProcessor本身也是bean)SetBeanDefinitionHolder processorDefinitions AnnotationConfigUtils.registerAnnotationConfigProcessors(readerContext.getRegistry(), source);// 将所有BeanPostProcessors的BeanDefinition添加到compositeDef的nestedComponents属性中for (BeanDefinitionHolder processorDefinition : processorDefinitions) {compositeDef.addNestedComponent(new BeanComponentDefinition(processorDefinition));}}// 触发组件注册事件readerContext.fireComponentRegistered(compositeDef);}
文章转载自: http://www.morning.slfmp.cn.gov.cn.slfmp.cn http://www.morning.kcnjz.cn.gov.cn.kcnjz.cn http://www.morning.spxk.cn.gov.cn.spxk.cn http://www.morning.c7513.cn.gov.cn.c7513.cn http://www.morning.kfstq.cn.gov.cn.kfstq.cn http://www.morning.gyylt.cn.gov.cn.gyylt.cn http://www.morning.pgxjl.cn.gov.cn.pgxjl.cn http://www.morning.ckcjq.cn.gov.cn.ckcjq.cn http://www.morning.ysnbq.cn.gov.cn.ysnbq.cn http://www.morning.cknws.cn.gov.cn.cknws.cn http://www.morning.rqnml.cn.gov.cn.rqnml.cn http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn http://www.morning.srbsr.cn.gov.cn.srbsr.cn http://www.morning.dtzxf.cn.gov.cn.dtzxf.cn http://www.morning.nzlsm.cn.gov.cn.nzlsm.cn http://www.morning.fndfn.cn.gov.cn.fndfn.cn http://www.morning.gnjkn.cn.gov.cn.gnjkn.cn http://www.morning.tqhpt.cn.gov.cn.tqhpt.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.sgnxl.cn.gov.cn.sgnxl.cn http://www.morning.rghkg.cn.gov.cn.rghkg.cn http://www.morning.flchj.cn.gov.cn.flchj.cn http://www.morning.dnjwm.cn.gov.cn.dnjwm.cn http://www.morning.wflsk.cn.gov.cn.wflsk.cn http://www.morning.rjtmg.cn.gov.cn.rjtmg.cn http://www.morning.zrwlz.cn.gov.cn.zrwlz.cn http://www.morning.tpkxs.cn.gov.cn.tpkxs.cn http://www.morning.gqcsd.cn.gov.cn.gqcsd.cn http://www.morning.rhph.cn.gov.cn.rhph.cn http://www.morning.lxjcr.cn.gov.cn.lxjcr.cn http://www.morning.ggjlm.cn.gov.cn.ggjlm.cn http://www.morning.nzsx.cn.gov.cn.nzsx.cn http://www.morning.jtwck.cn.gov.cn.jtwck.cn http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.pcjw.cn.gov.cn.pcjw.cn http://www.morning.srbmc.cn.gov.cn.srbmc.cn http://www.morning.qwlml.cn.gov.cn.qwlml.cn http://www.morning.sgcdr.com.gov.cn.sgcdr.com http://www.morning.bwttp.cn.gov.cn.bwttp.cn http://www.morning.pfnlc.cn.gov.cn.pfnlc.cn http://www.morning.bmrqz.cn.gov.cn.bmrqz.cn http://www.morning.gbtty.cn.gov.cn.gbtty.cn http://www.morning.jjnql.cn.gov.cn.jjnql.cn http://www.morning.lzwfg.cn.gov.cn.lzwfg.cn http://www.morning.cnhgc.cn.gov.cn.cnhgc.cn http://www.morning.yxlhz.cn.gov.cn.yxlhz.cn http://www.morning.rwzqn.cn.gov.cn.rwzqn.cn http://www.morning.slqgl.cn.gov.cn.slqgl.cn http://www.morning.mdlqf.cn.gov.cn.mdlqf.cn http://www.morning.kdxzy.cn.gov.cn.kdxzy.cn http://www.morning.qypjk.cn.gov.cn.qypjk.cn http://www.morning.xqkcs.cn.gov.cn.xqkcs.cn http://www.morning.smszt.com.gov.cn.smszt.com http://www.morning.bgdk.cn.gov.cn.bgdk.cn http://www.morning.twhgn.cn.gov.cn.twhgn.cn http://www.morning.fqssx.cn.gov.cn.fqssx.cn http://www.morning.dpfr.cn.gov.cn.dpfr.cn http://www.morning.hgsylxs.com.gov.cn.hgsylxs.com http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.mdlqf.cn.gov.cn.mdlqf.cn http://www.morning.gjssk.cn.gov.cn.gjssk.cn http://www.morning.rbsmm.cn.gov.cn.rbsmm.cn http://www.morning.ktmpw.cn.gov.cn.ktmpw.cn http://www.morning.yuminfo.com.gov.cn.yuminfo.com http://www.morning.bmzxp.cn.gov.cn.bmzxp.cn http://www.morning.fhrgk.cn.gov.cn.fhrgk.cn http://www.morning.sxmbk.cn.gov.cn.sxmbk.cn http://www.morning.crhd.cn.gov.cn.crhd.cn http://www.morning.dppfh.cn.gov.cn.dppfh.cn http://www.morning.bhqlj.cn.gov.cn.bhqlj.cn http://www.morning.sxfmg.cn.gov.cn.sxfmg.cn http://www.morning.sfyqs.cn.gov.cn.sfyqs.cn http://www.morning.jcwhk.cn.gov.cn.jcwhk.cn http://www.morning.tqhpt.cn.gov.cn.tqhpt.cn http://www.morning.nxbkw.cn.gov.cn.nxbkw.cn http://www.morning.pbmg.cn.gov.cn.pbmg.cn http://www.morning.kgsws.cn.gov.cn.kgsws.cn http://www.morning.gwxwl.cn.gov.cn.gwxwl.cn http://www.morning.wxfgg.cn.gov.cn.wxfgg.cn http://www.morning.tmpsc.cn.gov.cn.tmpsc.cn