ServiceBean的afterPropertiesSet方法是实现了InitializingBean,还是准备先做宏观分析,然后再做细致分析。下面先宏观分析:

  public void afterPropertiesSet() throws Exception {   
     if (getProvider() == null) {
          ..............
//获取provider配置
       }
       if (getApplication() == null && (getProvider() == null || getProvider().getApplication() == null)) {
   ...............
          //获取application配置
}
     if (getModule() == null && (getProvider() == null || getProvider().getModule() == null)) {
  ...............
          //获取module配置
}
if ((getRegistries() == null || getRegistries().size() == 0)
&& (getProvider() == null || getProvider().getRegistries() == null || getProvider().getRegistries().size() == 0)
&& (getApplication() == null || getApplication().getRegistries() == null || getApplication().getRegistries().size() == 0)) {
  .................
//获取注册中心的配置
}
if (getMonitor() == null
&& (getProvider() == null || getProvider().getMonitor() == null)
&& (getApplication() == null || getApplication().getMonitor() == null)) {
................
//获取monitor配置
}
if ((getProtocols() == null || getProtocols().size() == 0)
&& (getProvider() == null || getProvider().getProtocols() == null || getProvider().getProtocols().size() == 0)) {
...............
//获取protocol配置
}
if (getPath() == null || getPath().length() == 0) { //获取<dubbo:service/>的path属性,path即服务的发布路径
if (beanName != null && beanName.length() > 0
&& getInterface() != null && getInterface().length() > 0
&& beanName.startsWith(getInterface())) {
setPath(beanName); //如果没有设置path属性,则默认会以beanName作为path
}
}
if (! isDelay()) { //是否延迟暴露
export(); //进行服务暴露
}
}

通过上面的分析对整个方法在做什么有了大致的了解, 下面进行细致分析,对里面的一段段代码分别展开分析:

 1 . 获取Provider配置  

//当某个<dubbo:service/>没有绑定相应的<dubbo:provider/>的时候,就会触发下面的逻辑
if (getProvider() == null) {
     //在spring的IOC容器中查找所有的type为ProviderConfig.class或其子类的bean,可能会有多个provider的配置
     Map<String, ProviderConfig> providerConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProviderConfig.class, false, false);
        if (providerConfigMap != null && providerConfigMap.size() > 0) {
//在spring的IOC容器中查找type为ProtocolConfig.class或其子类的bean
Map<String, ProtocolConfig> protocolConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProtocolConfig.class, false, false);           //存在<dubbo:provider/>但不存在<dubbo:protocol/>配置的情况,也就是说旧版本的protocol配置需要从provider中提取
          if ((protocolConfigMap == null || protocolConfigMap.size() == 0)&& providerConfigMap.size() > 1) { // 兼容旧版本
List<ProviderConfig> providerConfigs = new ArrayList<ProviderConfig>();
            for (ProviderConfig config : providerConfigMap.values()) {
              if (config.isDefault() != null && config.isDefault().booleanValue()) {
providerConfigs.add(config); // 当<dubbo:provider default="true"/>时,providerConfigs才会加入
}
}
//在配置provider的同时,也从默认的<dubbo:provider/>中提取protocol的配置
            if (providerConfigs.size() > 0) {
setProviders(providerConfigs);
}
} else { //已存在<dubbo:protocol/>配置,则找出默认的<dubbo:provider/>配置
                    ProviderConfig providerConfig = null;
for (ProviderConfig config : providerConfigMap.values()) {
if (config.isDefault() == null || config.isDefault().booleanValue()) {
if (providerConfig != null) {
throw new IllegalStateException("Duplicate provider configs: " + providerConfig + " and " + config);
}
providerConfig = config;
}
}
if (providerConfig != null) {
setProvider(providerConfig);
}
}
}
}

在这里补充一下什么是默认的<dubbo:provider/>,在dubbo配置文件中,可以有多个<dubbo:provider/>配置,如果某个<dubbo:provide/>配置的default属性为true。这个默认配置只能一个。

2.获取application配置

这个相对比较容易,只是说明几点,用户可以通过<dubbo:application/>的形式来配置application,也可以直接以bean的形式去配,这个bean对应的Class就是ApplicationConfig.class

if (getApplication() == null
&& (getProvider() == null || getProvider().getApplication() == null)) {
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()) {
if (config.isDefault() == null || config.isDefault().booleanValue()) {
if (applicationConfig != null) {
throw new IllegalStateException("Duplicate application configs: " + applicationConfig + " and " + config);
}
applicationConfig = config;
}
}
if (applicationConfig != null) {
setApplication(applicationConfig);
}
}
}

3. 获取Module,同上

4. 获取Registries,同上

5. 获取Monitor , 同上

6. 获取Protocols ,同上

7. 获取path(服务路径)

if (getPath() == null || getPath().length() == 0) {
if (beanName != null && beanName.length() > 0
&& getInterface() != null && getInterface().length() > 0
&& beanName.startsWith(getInterface())) {
setPath(beanName);
}
}

上面代码说明如果<dubbo:service/>没有配path属性,dubbo将会设置一个默认的path属性,默认值就是beanName,而beanName是ServiceBean实现了BeanNameAware接口,由spring的IOC容器传入进来的。通过上文对dubbo配置的解析的源码分析可知,一般情况这个path属性就是服务接口的类的全路径名。

8. 判断该服务是否延迟发布

    if (! isDelay()) {

    export();   //服务暴露的方法
    }

   <dubbo:service/>上会有一个delay的配置属性(官方的说明:)

  


 private boolean isDelay() {
Integer delay = getDelay();
ProviderConfig provider = getProvider();
if (delay == null && provider != null) {
delay = provider.getDelay();
}
return supportedApplicationListener && (delay == null || delay.intValue() == -1);
}

上面为判断该服务是否延迟的方法的源码,说明如果<dubbo:service/>没有配delay属性或将其配置为-1都会延迟发布服务,而这个supportedApplicationListener属性在ServiceBean实现ApplicationContextAware接口的setApplicationContext方法中设置为true的。

下面再看一下setApplicationContext方法的源码,主要作用是为了兼容Spring2.0.1的版本,为了向前兼容spring的事件机制

public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
SpringExtensionFactory.addApplicationContext(applicationContext);
if (applicationContext != null) {
SPRING_CONTEXT = applicationContext;
try {
Method method = applicationContext.getClass().getMethod("addApplicationListener", new Class<?>[]{ApplicationListener.class}); // 兼容Spring2.0.1
method.invoke(applicationContext, new Object[] {this});
supportedApplicationListener = true;
} catch (Throwable t) {
if (applicationContext instanceof AbstractApplicationContext) {
try {
Method method = AbstractApplicationContext.class.getDeclaredMethod("addListener", new Class<?>[]{ApplicationListener.class}); // 兼容Spring2.0.1
if (! method.isAccessible()) {
method.setAccessible(true);
}
method.invoke(applicationContext, new Object[] {this});
supportedApplicationListener = true;
} catch (Throwable t2) {
}
}
}
}
}

根据这个源码可知,通常我们的dubbo服务的配置:<dubbo:service  interface="com.xxx.Yxxx" ref="userService" protocol="dubbo" retries="0" /> ,在这样的情况下 isDelay()方法返回true,所以我们的暴露将会发生在下面的方法中

 public void onApplicationEvent(ApplicationEvent event) {
if (ContextRefreshedEvent.class.getName().equals(event.getClass().getName())) {
if (isDelay() && ! isExported() && ! isUnexported()) {
if (logger.isInfoEnabled()) {
logger.info("The service ready on spring started. service: " + getInterface());
}
export();
}
}
}

至此,这个初始化的方法就分析完了。下文将开始分析 处理服务暴露的方法(export()方法)。

dubbo源码分析9——ServiceBean的afterPropertiesSet方法分析的更多相关文章

  1. dubbo源码分析4——SPI机制_ExtensionFactory类的作用

    ExtensionFactory的源码: @SPI public interface ExtensionFactory { /** * Get extension. * * @param type o ...

  2. Dubbo源码学习--服务发布(ServiceBean、ServiceConfig)

    前面讲过Dubbo SPI拓展机制,通过ExtensionLoader实现可插拔加载拓展,本节将接着分析Dubbo的服务发布过程. 以源码中dubbo-demo模块作为切入口一步步走进Dubbo源码. ...

  3. Dubbo 源码分析 - 服务导出

    1.服务导出过程 本篇文章,我们来研究一下 Dubbo 导出服务的过程.Dubbo 服务导出过程始于 Spring 容器发布刷新事件,Dubbo 在接收到事件后,会立即执行服务导出逻辑.整个逻辑大致可 ...

  4. dubbo源码分析8——服务暴露概述

    从上文中可知,com.alibaba.dubbo.config.spring.ServiceBean类是负责解析<dubbo:service/>的配置的,下面是它的类图 从类图上可知它继承 ...

  5. dubbo源码分析3-service bean的创建与发布

    dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...

  6. dubbo源码分析(一)-从xml到我们认识的Java对象

    项目中用的dubbo的挺多的,然后随着自己对dubbo的慢慢深入,自己也希望能够了解dubbo的底层实现,这半年来一直在看dubbo的源码,有点断断续续的,于是准备写一个dubbo源码系列的分析文章, ...

  7. dubbo源码分析6-telnet方式的管理实现

    dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...

  8. dubbo源码分析1-reference bean创建

    dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...

  9. dubbo源码分析2-reference bean发起服务方法调用

    dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...

随机推荐

  1. python金融反欺诈-项目实战

    python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...

  2. cmd命令对java程序进行编译时出现:编码GBK的不可映射字符

    原因:由于JDK是国际版的,在编译的时候,如果我们没有用-encoding参数指定JAVA源程序的编码格式,则java.exe首先获得我们才做系统默认采用的编码格式,也即在编译JAVA程序时,若我们不 ...

  3. float clearfix

    Float float 属性的原本作用是: 为了实现文字环绕效果 float 父元素高度塌陷实现文字环绕效果 float 固定一列宽的自适应布局 float 多列布局` float 固定一列宽的自适应 ...

  4. Trailing slash

    Trailing Slash common case It's common for URLs with a trailing slash to indicate a directory, and t ...

  5. 开源框架.netCore DncZeus学习(三)增加一个菜单

    框架运行起来了,先尝试增加一个菜单. 本节增加一个菜单名字:公司管理,需要注意一点,所有的name都要保持一致,注意圈中部分.为了防止手敲代码出错,建议复制已有的代代码进行修改(比如这里用的Role页 ...

  6. extjs.net 按钮执行并显示Mask代码

    <ext:Button ID="ButtonTest" runat="server"  Width="65" Text="同 ...

  7. 俄罗斯方块部分功能(Java)

    package OO.day01; public class TetrisCell { int totalRow = 20; int totalcol = 10; //定义横宽 int row; in ...

  8. mysql比较运算符和函数

    mysql> SELECT 15 BETWEEN 1 AND 22;+---------------------+| 15 BETWEEN 1 AND 22 |+---------------- ...

  9. Docker 更改默认存储目录 - 十一

    Cemtos 7 Docker 默认目录是 /var/lib/docker docker info 查看 docker 配置信息 更改 docker 默认目录 :  编辑 启动文件: 编辑 /usr/ ...

  10. Git学习一:基本用法

    git config:配置相关信息 git clone:复制仓库 git init:初始化仓库 git add:添加更新内容到索引中 git diff:比较内容 git status:获取当前项目状况 ...