Dubbo注解方式踩过的坑

2 篇文章 0 订阅

Dubbo注解方式踩过的坑

@Reference 正确的使用姿势

经过自己搭建的 SpringMVC + Dubbo 环境, Dubbo 的服务端可以正常注册服务,并且 Dubbo-admin 中也能正常查看到状态, 此时启动 Dubbo 消费端, Dubbo-admin 中可以正常显示消费端, 但是 Debug 跟断点的时候发现 @Reference 注解所注入的服务一直为 Null.

  • 想要解决这个问题 需要将 dubbo.xml 中的扫描器配置的位置移动到 SpringMVC 的子容器中即可解决.

代码块

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 配置扫描注入 -->
    <!-- 扫描controller(controller层注入) -->
    <context:component-scan base-package="com.fsdn"/>

    <!-- 会自动注册DefaultAnnotationHandleMapping与AnnotationMethodHandlerAdapter两个bean,是spring MVC为@Controllers分发请求所必须的 -->
    <!-- 指定自己定义的validator -->
    <mvc:annotation-driven validator="validator">
        <mvc:message-converters>
            <bean
                    class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg index="0" value="utf-8"/>
            </bean>
            <bean
                    class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
            <bean
                    class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
            <bean
                    class="org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter"/>
            <bean
                    class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
            <bean
                    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean
                            class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                        <property name="featuresToDisable">
                            <util:constant
                                    static-field="com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES"/>
                        </property>
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- 以下validator ConversionService在使用mvc:annotation-driven会自动注册 -->
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
        <!-- 如果不加默认到使用classpath下的ValidationMessages.properties -->
        <property name="validationMessageSource" ref="messageSource"/>
    </bean>

    <!-- 国际化的消息资源文件(本系统中主要用于显示/错误消息定制) -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <!-- 在web环境中一定要定位到classpath否则默认到当前web应用下找 -->
                <value>properties/ValidationMessages</value>
            </list>
        </property>
        <property name="useCodeAsDefaultMessage" value="false"/>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="cacheSeconds" value="60"/>
    </bean>

    <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
    </mvc:interceptors>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="zh_CN"/>
    </bean>

    <!-- 配置springMVC处理上传文件的信息 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"/>
        <property name="maxUploadSize" value="10485760000"/>
        <property name="maxInMemorySize" value="40960"/>
    </bean>

    <!-- 对模型视图添加前后缀 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
        <!--<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />-->
    </bean>

    <!-- 框架异常处理Handler -->
    <bean id="exceptionResolver" class="com.fsdn.framework.exception.SystemExceptionResolver"/>

    <import resource="classpath:spring/spring-dubbo.xml" />

</beans>

spring-dubbo.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 加载配置文件 -->
    <context:property-placeholder ignore-unresolvable="true"
            location="classpath*:properties/dubbo-common.properties,classpath*:properties/zookeeper.properties"/>

    <!-- 发布dubbo服务 -->
    <!-- 注册中心的地址 -->
    <dubbo:registry protocol="zookeeper" address="${zookeeper.address}" default="true" check="false"/>
    <dubbo:provider timeout="${dubbo.service.timeout}"/>
    <dubbo:monitor protocol="registry"/>

    <!-- 重点在这里, 一定要放到 SpringMVC 容器的配置文件中 -->
    <dubbo:annotation package="com.fsdn"/>

</beans>
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值