戴着假发的程序员出品
[查看视频教程]
注意这个属性和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 }