BeanDefinitionRegistryPostProcessor
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor { void postProcessBeanDefinitionRegistry ( BeanDefinitionRegistry registry) throws BeansException ;
}
BeanFactoryPostProcessor
@FunctionalInterface
public interface BeanFactoryPostProcessor { void postProcessBeanFactory ( ConfigurableListableBeanFactory var1) throws BeansException ;
}
BeanPostProcessor
public interface BeanPostProcessor { @Nullable default Object postProcessBeforeInitialization ( Object bean, String beanName) throws BeansException { return bean; } @Nullable default Object postProcessAfterInitialization ( Object bean, String beanName) throws BeansException { return bean; }
}
InitializingBean
public interface InitializingBean { void afterPropertiesSet ( ) throws Exception ;
}
DubboContextPostProcessor
public class DubboContextPostProcessor implements BeanDefinitionRegistryPostProcessor , ApplicationContextAware , EnvironmentAware { public static final String BEAN_NAME = "dubboContextPostProcessor" ; private ApplicationContext applicationContext; private ConfigurableEnvironment environment; @Override public void postProcessBeanFactory ( ConfigurableListableBeanFactory beanFactory) throws BeansException { ApplicationModel applicationModel = DubboBeanUtils . getApplicationModel ( beanFactory) ; ModuleModel moduleModel = DubboBeanUtils . getModuleModel ( beanFactory) ; SpringExtensionInjector . get ( applicationModel) . init ( applicationContext) ; SpringExtensionInjector . get ( moduleModel) . init ( applicationContext) ; DubboBeanUtils . getInitializationContext ( beanFactory) . setApplicationContext ( applicationContext) ; SortedMap < String , String > dubboProperties = EnvironmentUtils . filterDubboProperties ( environment) ; applicationModel. getModelEnvironment ( ) . getAppConfigMap ( ) . putAll ( dubboProperties) ; beanFactory. registerSingleton ( ConfigManager . BEAN_NAME , applicationModel. getApplicationConfigManager ( ) ) ; } @Override public void postProcessBeanDefinitionRegistry ( BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException { DubboSpringInitializer . initialize ( beanDefinitionRegistry) ; } @Override public void setApplicationContext ( ApplicationContext applicationContext) throws BeansException { this . applicationContext = applicationContext; } @Override public void setEnvironment ( Environment environment) { this . environment = ( ConfigurableEnvironment ) environment; }
}
DubboSpringInitializer
public class DubboSpringInitializer { private static final Logger logger = LoggerFactory . getLogger ( DubboSpringInitializer . class ) ; private static final Map < BeanDefinitionRegistry , DubboSpringInitContext > REGISTRY_CONTEXT_MAP = new ConcurrentHashMap < > ( ) ; public DubboSpringInitializer ( ) { } public static void initialize ( BeanDefinitionRegistry registry) { DubboSpringInitContext context = new DubboSpringInitContext ( ) ; if ( REGISTRY_CONTEXT_MAP . putIfAbsent ( registry, context) != null ) { return ; } ConfigurableListableBeanFactory beanFactory = findBeanFactory ( registry) ; initContext ( context, registry, beanFactory) ; } public static boolean remove ( BeanDefinitionRegistry registry) { return REGISTRY_CONTEXT_MAP . remove ( registry) != null ; } public static boolean remove ( ApplicationContext springContext) { AutowireCapableBeanFactory autowireCapableBeanFactory = springContext. getAutowireCapableBeanFactory ( ) ; for ( Map. Entry < BeanDefinitionRegistry , DubboSpringInitContext > entry : REGISTRY_CONTEXT_MAP . entrySet ( ) ) { DubboSpringInitContext initContext = entry. getValue ( ) ; if ( initContext. getApplicationContext ( ) == springContext|| initContext. getBeanFactory ( ) == autowireCapableBeanFactory|| initContext. getRegistry ( ) == autowireCapableBeanFactory) { DubboSpringInitContext context = REGISTRY_CONTEXT_MAP . remove ( entry. getKey ( ) ) ; logger. info ( "Unbind " + safeGetModelDesc ( context. getModuleModel ( ) ) + " from spring container: " + ObjectUtils . identityToString ( entry. getKey ( ) ) ) ; return true ; } } return false ; } static Map < BeanDefinitionRegistry , DubboSpringInitContext > getContextMap ( ) { return REGISTRY_CONTEXT_MAP ; } static DubboSpringInitContext findBySpringContext ( ApplicationContext applicationContext) { for ( DubboSpringInitContext initContext : REGISTRY_CONTEXT_MAP . values ( ) ) { if ( initContext. getApplicationContext ( ) == applicationContext) { return initContext; } } return null ; } private static void initContext ( DubboSpringInitContext context, BeanDefinitionRegistry registry, ConfigurableListableBeanFactory beanFactory) { context. setRegistry ( registry) ; context. setBeanFactory ( beanFactory) ; customize ( context) ; ModuleModel moduleModel = context. getModuleModel ( ) ; if ( moduleModel == null ) { ApplicationModel applicationModel; if ( findContextForApplication ( ApplicationModel . defaultModel ( ) ) == null ) { applicationModel = ApplicationModel . defaultModel ( ) ; logger. info ( "Use default application: " + applicationModel. getDesc ( ) ) ; } else { applicationModel = FrameworkModel . defaultModel ( ) . newApplication ( ) ; logger. info ( "Create new application: " + applicationModel. getDesc ( ) ) ; } moduleModel = applicationModel. getDefaultModule ( ) ; context. setModuleModel ( moduleModel) ; logger. info ( "Use default module model of target application: " + moduleModel. getDesc ( ) ) ; } else { logger. info ( "Use module model from customizer: " + moduleModel. getDesc ( ) ) ; } logger. info ( "Bind " + moduleModel. getDesc ( ) + " to spring container: " + ObjectUtils . identityToString ( registry) ) ; Map < String , Object > moduleAttributes = context. getModuleAttributes ( ) ; if ( moduleAttributes. size ( ) > 0 ) { moduleModel. getAttributes ( ) . putAll ( moduleAttributes) ; } registerContextBeans ( beanFactory, context) ; context. markAsBound ( ) ; moduleModel. setLifeCycleManagedExternally ( true ) ; if ( ! AotWithSpringDetector . useGeneratedArtifacts ( ) ) { DubboBeanUtils . registerCommonBeans ( registry) ; } } private static String safeGetModelDesc ( ScopeModel scopeModel) { return scopeModel != null ? scopeModel. getDesc ( ) : null ; } private static ConfigurableListableBeanFactory findBeanFactory ( BeanDefinitionRegistry registry) { ConfigurableListableBeanFactory beanFactory; if ( registry instanceof ConfigurableListableBeanFactory ) { beanFactory = ( ConfigurableListableBeanFactory ) registry; } else if ( registry instanceof GenericApplicationContext ) { GenericApplicationContext genericApplicationContext = ( GenericApplicationContext ) registry; beanFactory = genericApplicationContext. getBeanFactory ( ) ; } else { throw new IllegalStateException ( "Can not find Spring BeanFactory from registry: " + registry. getClass ( ) . getName ( ) ) ; } return beanFactory; } private static void registerContextBeans ( ConfigurableListableBeanFactory beanFactory, DubboSpringInitContext context) { if ( ! beanFactory. containsSingleton ( DubboSpringInitContext . class . getName ( ) ) ) { registerSingleton ( beanFactory, context) ; } if ( ! beanFactory. containsSingleton ( context. getApplicationModel ( ) . getClass ( ) . getName ( ) ) ) { registerSingleton ( beanFactory, context. getApplicationModel ( ) ) ; } if ( ! beanFactory. containsSingleton ( context. getModuleModel ( ) . getClass ( ) . getName ( ) ) ) { registerSingleton ( beanFactory, context. getModuleModel ( ) ) ; } } private static void registerSingleton ( ConfigurableListableBeanFactory beanFactory, Object bean) { beanFactory. registerSingleton ( bean. getClass ( ) . getName ( ) , bean) ; } private static DubboSpringInitContext findContextForApplication ( ApplicationModel applicationModel) { for ( DubboSpringInitContext initializationContext : REGISTRY_CONTEXT_MAP . values ( ) ) { if ( initializationContext. getApplicationModel ( ) == applicationModel) { return initializationContext; } } return null ; } private static void customize ( DubboSpringInitContext context) { Set < DubboSpringInitCustomizer > customizers = FrameworkModel . defaultModel ( ) . getExtensionLoader ( DubboSpringInitCustomizer . class ) . getSupportedExtensionInstances ( ) ; for ( DubboSpringInitCustomizer customizer : customizers) { customizer. customize ( context) ; } DubboSpringInitCustomizerHolder customizerHolder = DubboSpringInitCustomizerHolder . get ( ) ; customizers = customizerHolder. getCustomizers ( ) ; for ( DubboSpringInitCustomizer customizer : customizers) { customizer. customize ( context) ; } customizerHolder. clearCustomizers ( ) ; }
}
DubboConfigAliasPostProcessor
public class DubboConfigAliasPostProcessor implements BeanDefinitionRegistryPostProcessor , BeanPostProcessor { public static final String BEAN_NAME = "dubboConfigAliasPostProcessor" ; private BeanDefinitionRegistry registry; @Override public void postProcessBeanDefinitionRegistry ( BeanDefinitionRegistry registry) throws BeansException { this . registry = registry; } @Override public void postProcessBeanFactory ( ConfigurableListableBeanFactory beanFactory) throws BeansException { } @Override public Object postProcessBeforeInitialization ( Object bean, String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization ( Object bean, String beanName) throws BeansException { if ( bean instanceof AbstractConfig ) { String id = ( ( AbstractConfig ) bean) . getId ( ) ; if ( hasText ( id) && ! nullSafeEquals ( id, beanName) && ! BeanRegistrar . hasAlias ( registry, beanName, id) ) { registry. registerAlias ( beanName, id) ; } } return bean; }
}
DubboInfraBeanRegisterPostProcessor
public class DubboInfraBeanRegisterPostProcessor implements BeanDefinitionRegistryPostProcessor { public static final String BEAN_NAME = "dubboInfraBeanRegisterPostProcessor" ; private BeanDefinitionRegistry registry; @Override public void postProcessBeanDefinitionRegistry ( BeanDefinitionRegistry registry) throws BeansException { this . registry = registry; } @Override public void postProcessBeanFactory ( ConfigurableListableBeanFactory beanFactory) throws BeansException { if ( registry != null ) { ReferenceAnnotationBeanPostProcessor referenceAnnotationBeanPostProcessor = beanFactory. getBean ( ReferenceAnnotationBeanPostProcessor . BEAN_NAME , ReferenceAnnotationBeanPostProcessor . class ) ; beanFactory. addBeanPostProcessor ( referenceAnnotationBeanPostProcessor) ; DubboBeanUtils . registerPlaceholderConfigurerBeanIfNotExists ( beanFactory, registry) ; } if ( registry != null ) { registry. removeBeanDefinition ( BEAN_NAME ) ; } }
}
ServiceAnnotationPostProcessor
public class ServiceAnnotationPostProcessor implements BeanDefinitionRegistryPostProcessor , EnvironmentAware , ResourceLoaderAware , BeanClassLoaderAware , ApplicationContextAware , InitializingBean { public static final String BEAN_NAME = "dubboServiceAnnotationPostProcessor" ; private static final List < Class < ? extends Annotation > > serviceAnnotationTypes = loadServiceAnnotationTypes ( ) ; private static List < Class < ? extends Annotation > > loadServiceAnnotationTypes ( ) { if ( Dubbo2CompactUtils . isEnabled ( ) && Dubbo2CompactUtils . isServiceClassLoaded ( ) ) { return asList ( DubboService . class , Service . class , Dubbo2CompactUtils . getServiceClass ( ) ) ; } else { return asList ( DubboService . class , Service . class ) ; } } private final ErrorTypeAwareLogger logger = LoggerFactory . getErrorTypeAwareLogger ( getClass ( ) ) ; protected final Set < String > packagesToScan; private Set < String > resolvedPackagesToScan; private Environment environment; private ResourceLoader resourceLoader; private ClassLoader classLoader; private BeanDefinitionRegistry registry; protected ServicePackagesHolder servicePackagesHolder; private volatile boolean scanned = false ; public ServiceAnnotationPostProcessor ( String . . . packagesToScan) { this ( asList ( packagesToScan) ) ; } public ServiceAnnotationPostProcessor ( Collection < ? > packagesToScan) { this . packagesToScan = ( Set < String > ) packagesToScan. stream ( ) . collect ( Collectors . toSet ( ) ) ; } @Override public void afterPropertiesSet ( ) throws Exception { this . resolvedPackagesToScan = resolvePackagesToScan ( packagesToScan) ; } @Override public void postProcessBeanDefinitionRegistry ( BeanDefinitionRegistry registry) throws BeansException { this . registry = registry; scanServiceBeans ( resolvedPackagesToScan, registry) ; } @Override public void postProcessBeanFactory ( ConfigurableListableBeanFactory beanFactory) throws BeansException { if ( this . registry == null ) { this . registry = ( BeanDefinitionRegistry ) beanFactory; } String [ ] beanNames = beanFactory. getBeanDefinitionNames ( ) ; for ( String beanName : beanNames) { BeanDefinition beanDefinition = beanFactory. getBeanDefinition ( beanName) ; Map < String , Object > annotationAttributes = getServiceAnnotationAttributes ( beanDefinition) ; if ( annotationAttributes != null ) { processAnnotatedBeanDefinition ( beanName, ( AnnotatedBeanDefinition ) beanDefinition, annotationAttributes) ; } } if ( ! scanned) { scanServiceBeans ( resolvedPackagesToScan, registry) ; } } private void scanServiceBeans ( Set < String > packagesToScan, BeanDefinitionRegistry registry) { scanned = true ; if ( CollectionUtils . isEmpty ( packagesToScan) ) { if ( logger. isWarnEnabled ( ) ) { logger. warn ( CONFIG_NO_BEANS_SCANNED , "" , "" , "packagesToScan is empty , ServiceBean registry will be ignored!" ) ; } return ; } DubboClassPathBeanDefinitionScanner scanner = new DubboClassPathBeanDefinitionScanner ( registry, environment, resourceLoader) ; BeanNameGenerator beanNameGenerator = resolveBeanNameGenerator ( registry) ; scanner. setBeanNameGenerator ( beanNameGenerator) ; for ( Class < ? extends Annotation > annotationType : serviceAnnotationTypes) { scanner. addIncludeFilter ( new AnnotationTypeFilter ( annotationType) ) ; } ScanExcludeFilter scanExcludeFilter = new ScanExcludeFilter ( ) ; scanner. addExcludeFilter ( scanExcludeFilter) ; for ( String packageToScan : packagesToScan) { if ( servicePackagesHolder. isPackageScanned ( packageToScan) ) { if ( logger. isInfoEnabled ( ) ) { logger. info ( "Ignore package who has already bean scanned: " + packageToScan) ; } continue ; } if ( AotWithSpringDetector . useGeneratedArtifacts ( ) ) { scanner. setIncludeAnnotationConfig ( false ) ; } scanner. scan ( packageToScan) ; Set < BeanDefinitionHolder > beanDefinitionHolders = findServiceBeanDefinitionHolders ( scanner, packageToScan, registry, beanNameGenerator) ; if ( ! CollectionUtils . isEmpty ( beanDefinitionHolders) ) { if ( logger. isInfoEnabled ( ) ) { List < String > serviceClasses = new ArrayList < > ( beanDefinitionHolders. size ( ) ) ; for ( BeanDefinitionHolder beanDefinitionHolder : beanDefinitionHolders) { serviceClasses. add ( beanDefinitionHolder. getBeanDefinition ( ) . getBeanClassName ( ) ) ; } logger. info ( "Found " + beanDefinitionHolders. size ( ) + " classes annotated by Dubbo @Service under package [" + packageToScan + "]: " + serviceClasses) ; } for ( BeanDefinitionHolder beanDefinitionHolder : beanDefinitionHolders) { processScannedBeanDefinition ( beanDefinitionHolder) ; servicePackagesHolder. addScannedClass ( beanDefinitionHolder. getBeanDefinition ( ) . getBeanClassName ( ) ) ; } } else { if ( logger. isWarnEnabled ( ) ) { logger. warn ( CONFIG_NO_ANNOTATIONS_FOUND , "No annotations were found on the class" , "" , "No class annotated by Dubbo @DubboService or @Service was found under package [" + packageToScan + "], ignore re-scanned classes: " + scanExcludeFilter. getExcludedCount ( ) ) ; } } servicePackagesHolder. addScannedPackage ( packageToScan) ; } } private BeanNameGenerator resolveBeanNameGenerator ( BeanDefinitionRegistry registry) { BeanNameGenerator beanNameGenerator = null ; if ( registry instanceof SingletonBeanRegistry ) { SingletonBeanRegistry singletonBeanRegistry = SingletonBeanRegistry . class . cast ( registry) ; beanNameGenerator = ( BeanNameGenerator ) singletonBeanRegistry. getSingleton ( CONFIGURATION_BEAN_NAME_GENERATOR ) ; } if ( beanNameGenerator == null ) { if ( logger. isInfoEnabled ( ) ) { logger. info ( "BeanNameGenerator bean can't be found in BeanFactory with name [" + CONFIGURATION_BEAN_NAME_GENERATOR + "]" ) ; logger. info ( "BeanNameGenerator will be a instance of " + AnnotationBeanNameGenerator . class . getName ( ) + " , it maybe a potential problem on bean name generation." ) ; } beanNameGenerator = new AnnotationBeanNameGenerator ( ) ; } return beanNameGenerator; } private Set < BeanDefinitionHolder > findServiceBeanDefinitionHolders ( ClassPathBeanDefinitionScanner scanner, String packageToScan, BeanDefinitionRegistry registry, BeanNameGenerator beanNameGenerator) { Set < BeanDefinition > beanDefinitions = scanner. findCandidateComponents ( packageToScan) ; Set < BeanDefinitionHolder > beanDefinitionHolders = new LinkedHashSet < > ( beanDefinitions. size ( ) ) ; for ( BeanDefinition beanDefinition : beanDefinitions) { String beanName = beanNameGenerator. generateBeanName ( beanDefinition, registry) ; BeanDefinitionHolder beanDefinitionHolder = new BeanDefinitionHolder ( beanDefinition, beanName) ; beanDefinitionHolders. add ( beanDefinitionHolder) ; } return beanDefinitionHolders; } private void processScannedBeanDefinition ( BeanDefinitionHolder beanDefinitionHolder) { Class < ? > beanClass = resolveClass ( beanDefinitionHolder) ; Annotation service = findServiceAnnotation ( beanClass) ; Map < String , Object > serviceAnnotationAttributes = AnnotationUtils . getAttributes ( service, true ) ; String serviceInterface = resolveInterfaceName ( serviceAnnotationAttributes, beanClass) ; String annotatedServiceBeanName = beanDefinitionHolder. getBeanName ( ) ; String beanName = generateServiceBeanName ( serviceAnnotationAttributes, serviceInterface) ; AbstractBeanDefinition serviceBeanDefinition = buildServiceBeanDefinition ( serviceAnnotationAttributes, serviceInterface, annotatedServiceBeanName) ; registerServiceBeanDefinition ( beanName, serviceBeanDefinition, serviceInterface) ; } private Annotation findServiceAnnotation ( Class < ? > beanClass) { return serviceAnnotationTypes. stream ( ) . map ( annotationType -> ClassUtils . isPresent ( "org.springframework.core.annotation.AnnotatedElementUtils" , Thread . currentThread ( ) . getContextClassLoader ( ) ) && ReflectUtils . hasMethod ( org. springframework. core. annotation. AnnotatedElementUtils. class , "findMergedAnnotation" ) ? org. springframework. core. annotation. AnnotatedElementUtils. findMergedAnnotation ( beanClass, annotationType) : org. apache. dubbo. common. utils. AnnotationUtils. findAnnotation ( beanClass, annotationType) ) . filter ( Objects :: nonNull ) . findFirst ( ) . orElse ( null ) ; } private String generateServiceBeanName ( Map < String , Object > serviceAnnotationAttributes, String serviceInterface) { ServiceBeanNameBuilder builder = create ( serviceInterface, environment) . group ( ( String ) serviceAnnotationAttributes. get ( "group" ) ) . version ( ( String ) serviceAnnotationAttributes. get ( "version" ) ) ; return builder. build ( ) ; } private Class < ? > resolveClass ( BeanDefinitionHolder beanDefinitionHolder) { BeanDefinition beanDefinition = beanDefinitionHolder. getBeanDefinition ( ) ; return resolveClass ( beanDefinition) ; } private Class < ? > resolveClass ( BeanDefinition beanDefinition) { String beanClassName = beanDefinition. getBeanClassName ( ) ; return resolveClassName ( beanClassName, classLoader) ; } private Set < String > resolvePackagesToScan ( Set < String > packagesToScan) { Set < String > resolvedPackagesToScan = new LinkedHashSet < > ( packagesToScan. size ( ) ) ; for ( String packageToScan : packagesToScan) { if ( StringUtils . hasText ( packageToScan) ) { String resolvedPackageToScan = environment. resolvePlaceholders ( packageToScan. trim ( ) ) ; resolvedPackagesToScan. add ( resolvedPackageToScan) ; } } return resolvedPackagesToScan; } private AbstractBeanDefinition buildServiceBeanDefinition ( Map < String , Object > serviceAnnotationAttributes, String serviceInterface, String refServiceBeanName) { BeanDefinitionBuilder builder = rootBeanDefinition ( ServiceBean . class ) ; AbstractBeanDefinition beanDefinition = builder. getBeanDefinition ( ) ; beanDefinition. setAutowireMode ( AbstractBeanDefinition . AUTOWIRE_CONSTRUCTOR ) ; MutablePropertyValues propertyValues = beanDefinition. getPropertyValues ( ) ; String [ ] ignoreAttributeNames = ObjectUtils . of ( "provider" , "monitor" , "application" , "module" , "registry" , "protocol" , "methods" , "interfaceName" , "parameters" , "executor" ) ; propertyValues. addPropertyValues ( new AnnotationPropertyValuesAdapter ( serviceAnnotationAttributes, environment, ignoreAttributeNames) ) ; addPropertyReference ( builder, "ref" , refServiceBeanName) ; builder. addPropertyValue ( "interface" , serviceInterface) ; builder. addPropertyValue ( "parameters" , DubboAnnotationUtils . convertParameters ( ( String [ ] ) serviceAnnotationAttributes. get ( "parameters" ) ) ) ; List < MethodConfig > methodConfigs = convertMethodConfigs ( serviceAnnotationAttributes. get ( "methods" ) ) ; if ( ! methodConfigs. isEmpty ( ) ) { if ( AotWithSpringDetector . isAotProcessing ( ) ) { List < String > methodsJson = new ArrayList < > ( ) ; methodConfigs. forEach ( methodConfig -> methodsJson. add ( JsonUtils . toJson ( methodConfig) ) ) ; builder. addPropertyValue ( "methodsJson" , methodsJson) ; } else { builder. addPropertyValue ( "methods" , methodConfigs) ; } } String providerConfigId = ( String ) serviceAnnotationAttributes. get ( "provider" ) ; if ( StringUtils . hasText ( providerConfigId) ) { addPropertyValue ( builder, "providerIds" , providerConfigId) ; } String [ ] registryConfigIds = ( String [ ] ) serviceAnnotationAttributes. get ( "registry" ) ; if ( registryConfigIds != null && registryConfigIds. length > 0 ) { resolveStringArray ( registryConfigIds) ; builder. addPropertyValue ( "registryIds" , StringUtils . join ( registryConfigIds, ',' ) ) ; } String [ ] protocolConfigIds = ( String [ ] ) serviceAnnotationAttributes. get ( "protocol" ) ; if ( protocolConfigIds != null && protocolConfigIds. length > 0 ) { resolveStringArray ( protocolConfigIds) ; builder. addPropertyValue ( "protocolIds" , StringUtils . join ( protocolConfigIds, ',' ) ) ; } String monitorConfigId = ( String ) serviceAnnotationAttributes. get ( "monitor" ) ; if ( StringUtils . hasText ( monitorConfigId) ) { addPropertyReference ( builder, "monitor" , monitorConfigId) ; } String moduleConfigId = ( String ) serviceAnnotationAttributes. get ( "module" ) ; if ( StringUtils . hasText ( moduleConfigId) ) { addPropertyReference ( builder, "module" , moduleConfigId) ; } String executorBeanName = ( String ) serviceAnnotationAttributes. get ( "executor" ) ; if ( StringUtils . hasText ( executorBeanName) ) { addPropertyReference ( builder, "executor" , executorBeanName) ; } builder. setLazyInit ( false ) ; return builder. getBeanDefinition ( ) ; } private String [ ] resolveStringArray ( String [ ] strs) { if ( strs == null ) { return null ; } for ( int i = 0 ; i < strs. length; i++ ) { strs[ i] = environment. resolvePlaceholders ( strs[ i] ) ; } return strs; } private List convertMethodConfigs ( Object methodsAnnotation) { if ( methodsAnnotation == null ) { return Collections . EMPTY_LIST ; } return MethodConfig . constructMethodConfig ( ( Method [ ] ) methodsAnnotation) ; } private void addPropertyReference ( BeanDefinitionBuilder builder, String propertyName, String beanName) { String resolvedBeanName = environment. resolvePlaceholders ( beanName) ; builder. addPropertyReference ( propertyName, resolvedBeanName) ; } private void addPropertyValue ( BeanDefinitionBuilder builder, String propertyName, String value) { String resolvedBeanName = environment. resolvePlaceholders ( value) ; builder. addPropertyValue ( propertyName, resolvedBeanName) ; } private Map < String , Object > getServiceAnnotationAttributes ( BeanDefinition beanDefinition) { if ( beanDefinition instanceof AnnotatedBeanDefinition ) { AnnotatedBeanDefinition annotatedBeanDefinition = ( AnnotatedBeanDefinition ) beanDefinition; MethodMetadata factoryMethodMetadata = SpringCompatUtils . getFactoryMethodMetadata ( annotatedBeanDefinition) ; if ( factoryMethodMetadata != null ) { for ( Class < ? extends Annotation > annotationType : serviceAnnotationTypes) { if ( factoryMethodMetadata. isAnnotated ( annotationType. getName ( ) ) ) { Map < String , Object > annotationAttributes = factoryMethodMetadata. getAnnotationAttributes ( annotationType. getName ( ) ) ; return filterDefaultValues ( annotationType, annotationAttributes) ; } } } } return null ; } private void processAnnotatedBeanDefinition ( String refServiceBeanName, AnnotatedBeanDefinition refServiceBeanDefinition, Map < String , Object > attributes) { Map < String , Object > serviceAnnotationAttributes = new LinkedHashMap < > ( attributes) ; String returnTypeName = SpringCompatUtils . getFactoryMethodReturnType ( refServiceBeanDefinition) ; Class < ? > beanClass = resolveClassName ( returnTypeName, classLoader) ; String serviceInterface = resolveInterfaceName ( serviceAnnotationAttributes, beanClass) ; String serviceBeanName = generateServiceBeanName ( serviceAnnotationAttributes, serviceInterface) ; AbstractBeanDefinition serviceBeanDefinition = buildServiceBeanDefinition ( serviceAnnotationAttributes, serviceInterface, refServiceBeanName) ; serviceBeanDefinition. getPropertyValues ( ) . add ( Constants . ID , serviceBeanName) ; registerServiceBeanDefinition ( serviceBeanName, serviceBeanDefinition, serviceInterface) ; } private void registerServiceBeanDefinition ( String serviceBeanName, AbstractBeanDefinition serviceBeanDefinition, String serviceInterface) { if ( registry. containsBeanDefinition ( serviceBeanName) ) { BeanDefinition existingDefinition = registry. getBeanDefinition ( serviceBeanName) ; if ( existingDefinition. equals ( serviceBeanDefinition) ) { return ; } String msg = "Found duplicated BeanDefinition of service interface [" + serviceInterface+ "] with bean name [" + serviceBeanName + "], existing definition [ " + existingDefinition+ "], new definition [" + serviceBeanDefinition + "]" ; logger. error ( CONFIG_DUPLICATED_BEAN_DEFINITION , "" , "" , msg) ; throw new BeanDefinitionStoreException ( serviceBeanDefinition. getResourceDescription ( ) , serviceBeanName, msg) ; } registry. registerBeanDefinition ( serviceBeanName, serviceBeanDefinition) ; if ( logger. isInfoEnabled ( ) ) { logger. info ( "Register ServiceBean[" + serviceBeanName + "]: " + serviceBeanDefinition) ; } } @Override public void setEnvironment ( Environment environment) { this . environment = environment; } @Override public void setResourceLoader ( ResourceLoader resourceLoader) { this . resourceLoader = resourceLoader; } @Override public void setBeanClassLoader ( ClassLoader classLoader) { this . classLoader = classLoader; } @Override public void setApplicationContext ( ApplicationContext applicationContext) throws BeansException { this . servicePackagesHolder = applicationContext. getBean ( ServicePackagesHolder . BEAN_NAME , ServicePackagesHolder . class ) ; } private class ScanExcludeFilter implements TypeFilter { private int excludedCount; @Override public boolean match ( MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { String className = metadataReader. getClassMetadata ( ) . getClassName ( ) ; boolean excluded = servicePackagesHolder. isClassScanned ( className) ; if ( excluded) { excludedCount++ ; } return excluded; } public int getExcludedCount ( ) { return excludedCount; } }
}
org.springframework.context.annotation.ClassPathBeanDefinitionScanner#scan
org.springframework.context.annotation.ClassPathBeanDefinitionScanner#doScan
org.apache.dubbo.config.spring.context.annotation.DubboClassPathBeanDefinitionScanner#findCandidateComponents