Dubbo注解方式踩过的坑

  • Dubbo注解方式踩过的坑
    • Reference 正确的使用姿势
    • 代码块
      • spring-mvcxml
      • spring-dubboxml

Dubbo注解方式踩过的坑

@Reference 正确的使用姿势

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

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

代码块

spring-mvc.xml


<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">

    
    
    <context:component-scan base-package="com.fsdn"/>

    
    
    <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>

    
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
        
        <property name="validationMessageSource" ref="messageSource"/>
    bean>

    
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                
                <value>properties/ValidationMessagesvalue>
            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>

    
    <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"/>
        
    bean>

    
    <bean id="exceptionResolver" class="com.fsdn.framework.exception.SystemExceptionResolver"/>

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

beans>

spring-dubbo.xml


<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:registry protocol="zookeeper" address="${zookeeper.address}" default="true" check="false"/>
    <dubbo:provider timeout="${dubbo.service.timeout}"/>
    <dubbo:monitor protocol="registry"/>

    
    <dubbo:annotation package="com.fsdn"/>

beans>

你可能感兴趣的:(java,dubbo)