BeanDefinition有了,就会创建bean,bean的创建中,提到了如果实现了InitializingBean接口,就调用他的afterPropertiesSet方法,刚好,上面的bean有两个是实现了InitializingBean接口。分布是ServiceBeanReferenceBean,这边先看看ServiceBean。afterPropertiesSet方法主要是各种属性设置,包括ProviderConfig、ProtocolConfig、ApplicationConfig、ModuleConfig、registryIds、RegistryConfig、MetadataReportConfig、ConfigCenterConfig、MonitorConfig、MetricsConfig、ProtocolIds、ProtocolConfig、path。

ProviderConfig

如果没有定义ProviderConfig,则设置ProviderConfig和ProtocolConfig

if (getProvider() == null) {
    // 获取ProviderConfig类型的bean
    Map<String, ProviderConfig> providerConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProviderConfig.class, false, false);
    if (providerConfigMap != null && providerConfigMap.size() > 0) {
        // 如果ProviderConfig存在,获取ProtocolConfig类型的bean
        Map<String, ProtocolConfig> protocolConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProtocolConfig.class, false, false);
        if (CollectionUtils.isEmptyMap(protocolConfigMap)
                && providerConfigMap.size() > 1) { // backward compatibility
            // protocolConfigMap为空,且有多个ProviderConfig的时候,取默认
            List<ProviderConfig> providerConfigs = new ArrayList<ProviderConfig>();
            for (ProviderConfig config : providerConfigMap.values()) {
                if (config.isDefault() != null && config.isDefault()) {
                    providerConfigs.add(config);
                }
            }
            //把多个providerConfigs的值,赋值给ProtocolConfig
            if (!providerConfigs.isEmpty()) {
                setProviders(providerConfigs);
            }
        } else {
            // 这边只有两个情况:protocolConfigMap为空,providerConfigMap只有一个,或protocolConfigMap不为空,providerConfigMap有多个
            // 只能有一个默认的providerConfig
            ProviderConfig providerConfig = null;
            for (ProviderConfig config : providerConfigMap.values()) {
                if (config.isDefault() == null || config.isDefault()) {
                    if (providerConfig != null) {
                        throw new IllegalStateException("Duplicate provider configs: " + providerConfig + " and " + config);
                    }
                    providerConfig = config;
                }
            }
            if (providerConfig != null) {
                setProvider(providerConfig);
            }
        }
    }
}

ApplicationConfig

ApplicationConfig没有设置过,并且ProviderConfig的ApplicationConfig也没设置过的时候,设置ApplicationConfig,有且只有一个ApplicationConfig

if (getApplication() == null
            && (getProvider() == null || getProvider().getApplication() == null)) {
    // 获取ApplicationConfig类型的bean
    Map<String, ApplicationConfig> applicationConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ApplicationConfig.class, false, false);
    if (applicationConfigMap != null && applicationConfigMap.size() > 0) {
        ApplicationConfig applicationConfig = null;
        for (ApplicationConfig config : applicationConfigMap.values()) {
            // 只能设置一个ApplicationConfig
            if (applicationConfig != null) {
                throw new IllegalStateException("Duplicate application configs: " + applicationConfig + " and " + config);
            }
            applicationConfig = config;
        }
        if (applicationConfig != null) {
            setApplication(applicationConfig);
        }
    }
}

ModuleConfig

没有设置过,并且ProviderConfig的ModuleConfig也没设置过的时候,设置ModuleConfig,有且只有一个ModuleConfig

if (getModule() == null
            && (getProvider() == null || getProvider().getModule() == null)) {
    // 获取ModuleConfig类型的bean
    Map<String, ModuleConfig> moduleConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ModuleConfig.class, false, false);
    if (moduleConfigMap != null && moduleConfigMap.size() > 0) {
        ModuleConfig moduleConfig = null;
        for (ModuleConfig config : moduleConfigMap.values()) {
            // // 只能设置一个默认的ModuleConfig
            if (config.isDefault() == null || config.isDefault()) {
                if (moduleConfig != null) {
                    throw new IllegalStateException("Duplicate module configs: " + moduleConfig + " and " + config);
                }
                moduleConfig = config;
            }
        }
        if (moduleConfig != null) {
            setModule(moduleConfig);
        }
    }
}

registryIds

设置registryIds,如果都有值,ProviderConfig会覆盖ApplicationConfig的registryIds

if (StringUtils.isEmpty(getRegistryIds())) {
    if (getApplication() != null && StringUtils.isNotEmpty(getApplication().getRegistryIds())) {
        setRegistryIds(getApplication().getRegistryIds());
    }
    if (getProvider() != null && StringUtils.isNotEmpty(getProvider().getRegistryIds())) {
        setRegistryIds(getProvider().getRegistryIds());
    }
}

RegistryConfig

如果ServiceBean、ProviderConfig、ApplicationConfig都没有RegistryConfig信息,则通过id从spring容器添加,如果spring容器也没有对应的id,则加个默认的

if ((CollectionUtils.isEmpty(getRegistries()))
            && (getProvider() == null || CollectionUtils.isEmpty(getProvider().getRegistries()))
            && (getApplication() == null || CollectionUtils.isEmpty(getApplication().getRegistries()))) {
    // 获取RegistryConfig类型的bean
    Map<String, RegistryConfig> registryConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, RegistryConfig.class, false, false);
    if (CollectionUtils.isNotEmptyMap(registryConfigMap)) {
        // 遍历判断id,把spring容器的RegistryConfig添加到registryConfigs
        List<RegistryConfig> registryConfigs = new ArrayList<>();
        if (StringUtils.isNotEmpty(registryIds)) {
            Arrays.stream(COMMA_SPLIT_PATTERN.split(registryIds)).forEach(id -> {
                if (registryConfigMap.containsKey(id)) {
                    registryConfigs.add(registryConfigMap.get(id));
                }
            });
        }
        // 如果还是空,把默认的加入到registryConfigs
        if (registryConfigs.isEmpty()) {
            for (RegistryConfig config : registryConfigMap.values()) {
                if (StringUtils.isEmpty(registryIds) && (config.isDefault() == null || config.isDefault().booleanValue())) {
                    registryConfigs.add(config);
                }
            }
        }
        // 设置到Registries
        if (!registryConfigs.isEmpty()) {
            super.setRegistries(registryConfigs);
        }
    }
}

MetadataReportConfig

if (getMetadataReportConfig() == null) {
    // 获取MetadataReportConfig类型的bean,只能设置一个
    Map<String, MetadataReportConfig> metadataReportConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, MetadataReportConfig.class, false, false);
    if (metadataReportConfigMap != null && metadataReportConfigMap.size() == 1) {
        super.setMetadataReportConfig(metadataReportConfigMap.values().iterator().next());
    } else if (metadataReportConfigMap != null && metadataReportConfigMap.size() > 1) {
        throw new IllegalStateException("Multiple MetadataReport configs: " + metadataReportConfigMap);
    }
}

ConfigCenterConfig

if (getConfigCenter() == null) {
    // 获取ConfigCenterConfig类型的bean,只能设置一个
    Map<String, ConfigCenterConfig> configenterMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ConfigCenterConfig.class, false, false);
    if (configenterMap != null && configenterMap.size() == 1) {
        super.setConfigCenter(configenterMap.values().iterator().next());
    } else if (configenterMap != null && configenterMap.size() > 1) {
        throw new IllegalStateException("Multiple ConfigCenter found:" + configenterMap);
    }
}

MonitorConfig

if (getMonitor() == null
            && (getProvider() == null || getProvider().getMonitor() == null)
            && (getApplication() == null || getApplication().getMonitor() == null)) {
    // 获取MonitorConfig类型的bean,只能设置一个默认
    Map<String, MonitorConfig> monitorConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, MonitorConfig.class, false, false);
    if (monitorConfigMap != null && monitorConfigMap.size() > 0) {
        MonitorConfig monitorConfig = null;
        for (MonitorConfig config : monitorConfigMap.values()) {
            if (config.isDefault() == null || config.isDefault()) {
                if (monitorConfig != null) {
                    throw new IllegalStateException("Duplicate monitor configs: " + monitorConfig + " and " + config);
                }
                monitorConfig = config;
            }
        }
        if (monitorConfig != null) {
            setMonitor(monitorConfig);
        }
    }
}

MetricsConfig

if (getMetrics() == null) {
    // 获取MetricsConfig类型的bean,只能设置一个
    Map<String, MetricsConfig> metricsConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, MetricsConfig.class, false, false);
    if (metricsConfigMap != null && metricsConfigMap.size() > 0) {
        MetricsConfig metricsConfig = null;
        for (MetricsConfig config : metricsConfigMap.values()) {
            if (metricsConfig != null) {
                throw new IllegalStateException("Duplicate metrics configs: " + metricsConfig + " and " + config);
            }
            metricsConfig = config;
        }
        if (metricsConfig != null) {
            setMetrics(metricsConfig);
        }
    }
}

ProtocolIds

if (StringUtils.isEmpty(getProtocolIds())) {
    if (getProvider() != null && StringUtils.isNotEmpty(getProvider().getProtocolIds())) {
        setProtocolIds(getProvider().getProtocolIds());
    }
}

ProtocolConfig

如果ServiceBean、ProviderConfig都没有ProtocolConfig信息,则通过id从spring容器添加,如果spring容器也没有对应的id,则加个默认的

if (CollectionUtils.isEmpty(getProtocols())
            && (getProvider() == null || CollectionUtils.isEmpty(getProvider().getProtocols()))) {
    Map<String, ProtocolConfig> protocolConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProtocolConfig.class, false, false);
    if (protocolConfigMap != null && protocolConfigMap.size() > 0) {
        List<ProtocolConfig> protocolConfigs = new ArrayList<ProtocolConfig>();
        if (StringUtils.isNotEmpty(getProtocolIds())) {
            Arrays.stream(COMMA_SPLIT_PATTERN.split(getProtocolIds()))
                    .forEach(id -> {
                        if (protocolConfigMap.containsKey(id)) {
                            protocolConfigs.add(protocolConfigMap.get(id));
                        }
                    });
        }
        // 如果还是空,把默认的加入到protocolConfigs
        if (protocolConfigs.isEmpty()) {
            for (ProtocolConfig config : protocolConfigMap.values()) {
                if (StringUtils.isEmpty(protocolIds)) {
                    protocolConfigs.add(config);
                }
            }
        }

        if (!protocolConfigs.isEmpty()) {
            super.setProtocols(protocolConfigs);
        }
    }
}

path

设置path

if (StringUtils.isEmpty(getPath())) {
    if (StringUtils.isNotEmpty(beanName)
            && StringUtils.isNotEmpty(getInterface())
            && beanName.startsWith(getInterface())) {
        setPath(beanName);
    }
}

服务暴露export

supportedApplicationListener在setApplicationContext方法设置的,因为这个类还实现了ApplicationContextAware接口

if (!supportedApplicationListener) {
    export();
}

后面我们看看服务是怎么暴露的


大军
847 声望183 粉丝

学而不思则罔,思而不学则殆