2.30@ComponentScan注解的scopeResolver属性

戴着假发的程序员出品  抖音ID:戴着假发的程序员  欢迎关注

[查看视频教程]

这里的scopeResolver也是和 context:component-scan的scope-resolver属性完全一致:

注意这个属性和scoped-proxy是互斥的。

这个属性跟name-generator有点类似,它是基于接口ScopeMetadataResolver的,实现resolveScopeMetadata方法,目的是为了将@Scope(value="",proxyMode=ScopedProxyMode.NO,scopeName="")的配置解析成为一个ScopeMetadata对象,Spring这里也提供了两个实现,我们一起看下。首先是org.springframework.context.annotation.AnnotationScopeMetadataResolver中,

 1 public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
 2         ScopeMetadata metadata = new ScopeMetadata();
 3         if (definition instanceof AnnotatedBeanDefinition) {
 4             AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;
 5             AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(annDef.getMetadata(), this.scopeAnnotationType);
 6             if (attributes != null) {
 7                 metadata.setScopeName(attributes.getAliasedString("value", this.scopeAnnotationType, definition.getSource()));
 8                 ScopedProxyMode proxyMode = attributes.getEnum("proxyMode");
 9                 if (proxyMode == null || proxyMode == ScopedProxyMode.DEFAULT) {
10                     proxyMode = this.defaultProxyMode;
11                 }
12                 metadata.setScopedProxyMode(proxyMode);
13             }
14         }
15         return metadata;
16     }

org.springframework.context.annotation.Jsr330ScopeMetadataResolver中的实现:

 1 public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
 2         ScopeMetadata metadata = new ScopeMetadata();
 3         metadata.setScopeName(BeanDefinition.SCOPE_PROTOTYPE);
 4         if (definition instanceof AnnotatedBeanDefinition) {
 5             AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;
 6             Set<String> annTypes = annDef.getMetadata().getAnnotationTypes();
 7             String found = null;
 8             for (String annType : annTypes) {
 9                 Set<String> metaAnns = annDef.getMetadata().getMetaAnnotationTypes(annType);
10                 if (metaAnns.contains("javax.inject.Scope")) {
11                     if (found != null) {
12                         throw new IllegalStateException("Found ambiguous scope annotations on bean class [" +
13                                 definition.getBeanClassName() + "]: " + found + ", " + annType);
14                     }
15                     found = annType;
16                     String scopeName = resolveScopeName(annType);
17                     if (scopeName == null) {
18                         throw new IllegalStateException(
19                                 "Unsupported scope annotation - not mapped onto Spring scope name: " + annType);
20                     }
21                     metadata.setScopeName(scopeName);
22                 }
23             }
24         }
25         return metadata;
26     }

 

posted @ 2020-10-13 08:14  戴着假发的程序员0-1  阅读(298)  评论(0编辑  收藏  举报