出現空指針的原因是:spring mvc掃描的時候根本無法識別@Reference ,同一方面,dubbo的掃描也無法識別Spring @Controller ,所以兩個掃描的順序要排列好, 如果先掃了controller,這時候把控制器都實例化好了,再掃dubbo的服務,就會出現空指針。
將springMVC的配置與dubbo配置寫在一個配置文件中,注意順序
<mvc:annotation-driven /> <!-- 消費方應用名,用於計算依賴關系,不是匹配條件,不要與提供方一樣 --> <dubbo:application name="dubbo-consumer" owner="programmer" organization="dubbox" /> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:consumer timeout="30000" /> <!-- <dubbo:reference id="demoService" interface="com.dubbo.service.DemoService"/> --> <dubbo:annotation package="com.dubbo.controller" /> <!-- 把標記了@Controller注解的類轉換為bean --> <context:component-scan base-package="com.dubbo.controller" />