page contents

Dubbo测试 @Autowired和@Resource均无法注入

Pack 发布于 2020-01-20 16:03
阅读 985
收藏 0
分类:Java开发

问题描述

在对Dubbo进行测试的过程中,启动好了provider,在consumer端,采用ClassPathXmlApplicationContext启动方式。
问题:
直接从context中获取provider的实例是可以获取到的,调用也是成功的;
但是我这里定义了一个TradeService接口,并在TradeServiceImpl类中实现了该接口,类中采用了@Autowire或@Resource方式进行注入,但注入结果均为null,对比了一下公司环境中配置,发现没有什么不同的,请问这里是什么问题呢?有没有人遇到过类似的问题?

环境及代码

  • JDK1.8
  • Dubbo 2.7.3
  • 配置方式:namespace, 即applicatoin.xml

consumer配置:



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

<!--应用名称, 用于计算依赖关系--> <dubbo:application name="dubbo-example-namespace-consumer" logger="slf4j"/>

<!--注册中心地址--> <dubbo:registry address="zookeeper://127.0.0.1:2181" file="dubbo/dubbo-registry/dubbo.cache"/>

<dubbo:protocol name="dubbo" port="20880"/> <dubbo:reference id="tradeServiceFacade" interface="com.lthaoshao.dubbo.api.TradeServiceFacade"/>

<bean id="orderService" class="com.lthaoshao.dubbo.service.OrderServiceImpl"/> </beans>

接口:



public interface IOrderService { public String createOrder(); }

实现类:



import com.lthaoshao.dubbo.api.TradeServiceFacade; import com.lthaoshao.dubbo.request.BaseRequest; import com.lthaoshao.dubbo.response.BaseResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired;

@Slf4j public class OrderServiceImpl implements IOrderService { // TODO 疑问点: 此处的tradeServiceFacade为null @Autowired private TradeServiceFacade tradeServiceFacade;

public OrderServiceImpl() { } @Override public String createOrder() { BaseRequest request = new BaseRequest(); request.setRequestId(String.valueOf(System.currentTimeMillis())); BaseResponse result = tradeServiceFacade.createOrder(request); log.info("response:{}", result); return result.getMsg(); } }

启动类:



import com.lthaoshao.dubbo.api.TradeServiceFacade; import com.lthaoshao.dubbo.request.BaseRequest; import com.lthaoshao.dubbo.response.BaseResponse; import com.lthaoshao.dubbo.service.IOrderService; import lombok.extern.slf4j.Slf4j; import org.springframework.context.support.ClassPathXmlApplicationContext;

@Slf4j public class DubboConsumerStart { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); context.start(); // 1. 直接从context中获取远程服务的实例, 此种方式可以获取到bean TradeServiceFacade tradeServiceFacade = context.getBean(TradeServiceFacade.class); BaseRequest request = new BaseRequest(); request.setRequestId(String.valueOf(System.currentTimeMillis())); BaseResponse result = tradeServiceFacade.createOrder(request); log.info("response:{}", result);

// 2. 通过服务调用, Autowired注入, 此种方式不能获取到实例 IOrderService orderService = context.getBean(IOrderService.class); String order = orderService.createOrder(); log.info("order:{}", order); } }


233
Pack
Pack

我拉了你的代码,你的pom 似乎有问题,我改为这样


<project xmlns="http://maven.apache.org/POM/4.0.0"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>

        <artifactId>dubbo-example-namespace</artifactId>

        <groupId>com.lthaoshao</groupId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>


    <artifactId>dubbo-consumer</artifactId>


    <dependencies>

        <dependency>

            <groupId>org.apache.dubbo</groupId>

            <artifactId>dubbo</artifactId>

        </dependency>

        <dependency>

            <groupId>org.apache.dubbo</groupId>

            <artifactId>dubbo-registry-zookeeper</artifactId>

            <version>2.7.0</version>

        </dependency>

        <dependency>

            <groupId>org.apache.dubbo</groupId>

            <artifactId>dubbo-configcenter-zookeeper</artifactId>

            <version>2.7.0</version>

        </dependency>

        <dependency>

            <groupId>com.lthaoshao</groupId>

            <artifactId>dubbo-api</artifactId>

        </dependency>

        <dependency>

            <groupId>org.slf4j</groupId>

            <artifactId>slf4j-api</artifactId>

            <scope>compile</scope>

        </dependency>

        <dependency>

            <groupId>ch.qos.logback</groupId>

            <artifactId>logback-classic</artifactId>

        </dependency>

    </dependencies>


</project>


<project xmlns="http://maven.apache.org/POM/4.0.0"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>

        <artifactId>dubbo-example-namespace</artifactId>

        <groupId>com.lthaoshao</groupId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>


    <artifactId>dubbo-provider</artifactId>


    <dependencies>

        <dependency>

            <groupId>org.apache.dubbo</groupId>

            <artifactId>dubbo</artifactId>

            <version>2.7.0</version>

        </dependency>

        <dependency>

            <groupId>org.apache.dubbo</groupId>

            <artifactId>dubbo-registry-zookeeper</artifactId>

            <version>2.7.0</version>

        </dependency>

        <dependency>

            <groupId>org.apache.dubbo</groupId>

            <artifactId>dubbo-configcenter-zookeeper</artifactId>

            <version>2.7.0</version>

        </dependency>


        <dependency>

            <groupId>com.lthaoshao</groupId>

            <artifactId>dubbo-api</artifactId>

        </dependency>

        <dependency>

            <groupId>org.slf4j</groupId>

            <artifactId>slf4j-api</artifactId>

        </dependency>

        <dependency>

            <groupId>ch.qos.logback</groupId>

            <artifactId>logback-classic</artifactId>

        </dependency>

    </dependencies>


</project>


改了依赖之后调用是没有问题的。

如果你试了还有问题欢迎私信我

请先 登录 后评论