【applicationContext-service.xml配置文件】java
在applicationContext-service.xml中須要配置基本的包掃描,且使用一個服務就要暴露一個 服務接口web
<context:component-scan base-package="com.pinyougou.sellergoods.service.impl" /> <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol> <dubbo:application name="pinyougou-sellergoods-service"/>
<dubbo:registry address="zookeeper://192.168.25.148:2181"/>
<!-- 聲明須要暴露的服務接口 --> <dubbo:service interface="com.pinyougou.sellergoods.service.BrandService" ref="brandServiceImpl" timeout="600000"/> <dubbo:service interface="com.pinyougou.sellergoods.service.SpecificationService" ref="specificationServiceImpl" timeout="600000"/>
【impl實現類中使用@Service註解】spring
這裏的@Service來自org.springframework.stereotype.Service;包下mvc
【springmvc.xml】app
在springmvc.xml中也須要配置基本的包掃描,且使用哪一個服務就引用哪一個服務spa
<context:component-scan base-package="com.pinyougou.manager.controller" /> <!-- 引用dubbo 服務 --> <dubbo:application name="pinyougou-manager-web" /> <dubbo:registry address="zookeeper://192.168.25.148:2181"/> <dubbo:reference interface="com.pinyougou.sellergoods.service.BrandService" id="brandService" /> <dubbo:reference interface="com.pinyougou.sellergoods.service.SpecificationService" id="specificationService" />
【在controller中引用service服務時使用@Resource註解】.net
@Resource private BrandService brandService ;
@Resource來自於javax.annotation.Resource包下code
說明 : 使用此種方式不須要暴露服務接口,也不須要引用服務接口,直接使用dubbo註解便可component
【applicationContext-service.xml配置文件】xml
<dubbo:protocol name="dubbo" port="20881"></dubbo:protocol> <dubbo:application name="pinyougou-sellergoods-service"/> <dubbo:registry address="zookeeper://192.168.25.148:2181"/> <dubbo:annotation package="com.pinyougou.sellergoods.service.impl" />
【impl實現類使用@service註解】
注意,@Service來自com.alibaba.dubbo.config.annotation.Service;包下
【springmvc.xml】
<!-- 引用dubbo 服務 --> <dubbo:application name="pinyougou-manager-web" /> <dubbo:registry address="zookeeper://192.168.25.148:2181"/> <dubbo:annotation package="com.pinyougou.manager.controller" />
【在controller中使用@Reference引用服務】
@Reference private BrandService brandService ;
@Reference來自com.alibaba.dubbo.config.annotation.Reference;包下