org.springframework.stereotype.Service和com.alibaba.dubbo.config.annotation.Service兩種service的區別


這兩個Service,都可以在service類頭上使用@Service的注解,於是我就寫錯了,查了半天才發現。
他們的區別大概是這個樣子的:

 

org.springframework.stereotype.Service

org.springframework.stereotype.Service是spring的注解,用於標注業務層組件(service)。
標注了的組件需要在配置文件里有這樣的配置<context:component-scan base-package="這里是包名" ></context:component-scan>

 

spring其他的一些差不多功能的注解還有這些:@Repository、@Controller、@Component。
@Service用於標注業務層組件(我們通常定義的service層就用這個)
@Controller用於標注控制層組件(如struts中的action)
@Repository用於標注數據訪問組件,即DAO組件
@Component泛指組件,當組件不好歸類的時候,我們可以使用這個注解進行標注。

另外還有其他幾個用於注入的注解,他們是@Resource、@Autowired、@Qualifier
1,@Resource

     來自javax.annotation.Resource,屬於javaEE,並不是spring中的,可以使用@Resource("xxx")來可以定義bean名稱。

2,@Autowired

     來自org.springframework.beans.factory.annotation.Autowired這個包中,它是spring的包,沒有@Autowired("xxx")這樣的方式定義bean名稱。

3,@Qualifier

     來自org.springframework.beans.factory.annotation.Qualifier,是spring中的注解,可以用@Qualifier("xxx"),在有多個實現的時候會用到。

 

com.alibaba.dubbo.config.annotation.Service
com.alibaba.dubbo.config.annotation.Service是dubbo用於聲明provider的注解。
標注了這個注解的service需要在配置文件里添加這樣的配置:<dubbo:annotation package="這里是包名" /> 

在dubbo中定義了4種關於provider和consumer的配置方式,分別是:
1. XML 配置文件方式。
   XML里配context:component-scan,dubbo:application,dubbo:registry,dubbo:protocol,dubbo:service或dubbo:reference。
2. properties 配置文件方式。
   XML里只剩下了dubbo:service或dubbo:reference,在property文件里配dubbo.application.name,dubbo.registry.address,dubbo.protocol.name,dubbo.protocol.port。
3. annotation 配置方式。
   配置文件中的dubbo:service或dubbo:reference統一替換成了dubbo:annotation,provider的實現類使用@Service注解,consumer中使用@Reference注解。

   另外,如果有加載context:component-scan的controller,那么dubbo:annotation的配置應在controller之前,否則provider的service會加載失敗。

4. API 配置方式。
   API僅用於OpenAPI, ESB, Test, Mock等系統集成,配置是在代碼中寫死的,比如:ApplicationConfig.setName("xxx") 對應 <dubbo:application name="xxx" />

 

另外,如果既需要spring注入又需要當做provider,不能同時使用這兩個service注解,因為同名不能import,真要用的話spring的那個可以換成@Component。

 

 

 

 

 

 

 


---------------------
作者:lkforce
來源:CSDN
原文:https://blog.csdn.net/lkforce/article/details/52983345
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM