<context:component-scan 和 <dubbo:annotation 有冲突??
在spring mvc配置文件中添加<context:component-scan 扫描controller,dubbo消费端就无法注入到zk中,把<context:component-scan 去掉dubbo就能正常注入,这两个配置有冲突么?两个配置扫描的package是一样的
你可以看下你web.xml下面的这两个配置
<!-- spring监听 读取的配置文件地址 可以添加多个配置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext.xml
</param-value>
</context-param>
<!-- springmvc配置文件 -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
先读取applicationContext.xml这个配置文件,再读取spring-context.xml。其中如果spring-context.xml这个配置文件配置context:component-scan 但是没有去加载spring.handlers下面http\://code.alibabatech.com/schema/dubbo=com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler 这个去解析dubbo的@Reference注解。就会导致注解@Reference这个属性为空。
可以去看下spring DefaultNamespaceHandlerResolver这个类
@li362692680 具体<context:component-scan 、<dubbo:annotation的配置贴一下吧
<!-- 扫描 controller-->
<context:component-scan base-package="com.fominfo.**.controller" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="regex" expression=".*Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubbo-service-consumer" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:annotation package="com.fominfo" />
要把controller扫描配置去掉duubo消费者才能注册到zk中 @chickenlj
你的web工程是如@zhangyinyue 提到的两级applicationcontext配置吗? 会不会是<context:component-scan 配置有问题,导致某些bean扫描不到,检查一下注册不上去有没有报其他错误?
@li362692680 <context:component-scan 属于 Spring MVC 范畴(spring-context.xml),<dubbo:annotation 应该属于 Spring 框架范畴(applicationContext.xml),即Dubbo组件的生命周期管理由Spring容器托管。我们都是把两者配置拆成两个XML配置的,如果真解决不了,建议把类似的简版代码上传到GitHub上,贴一下地址
解决了 @EdwardLee03 @chickenlj ,把dubbo的扫描放到applicationContext.xml中就好了,多谢各位,麻烦你们了。