Dubbo 2.7 整合Spring(注解形式)

本文的代码是在 Dubbo 2.7 整合Spring(xml形式)的基础上进行改动,主要代码不变。

项目结构:

dubbo-spring-storage

1、applicationContext.xml去掉<dubbo:service/>的配置,添加DubboConfig.java

@Configuration
@DubboComponentScan(basePackages = {"com.harvey"})
public class DubboConfig {

}

2、使用@DubboService注解暴露接口

@DubboService(protocol = "dubbo")
public class StorageReduceDubboServiceImpl implements StorageReduceDubboService {
}

dubbo-spring-order

1、applicationContext.xml去掉<dubbo:service/>的配置,添加DubboConfig.java

@Configuration
@DubboComponentScan(basePackages = {"com.harvey"})
public class DubboConfig {

}

2、使用@DubboReference注解引用接口(原来是使用Spring的@Autowired注解)

@DubboReference
private StorageReduceDubboService storageReduceDubboService;

说明:原以为使用<dubbo:annotation/>配置包路径可以达到同样的效果,但是在@DubboReference注解的变量是空的。

相关代码可以下载:提取代码

 

posted @ 2022-03-27 22:07  残城碎梦  阅读(122)  评论(0编辑  收藏  举报