首页 > 调用dubbo接口,用xml实例化bean时报No bean named 'XXX' is defined

调用dubbo接口,用xml实例化bean时报No bean named 'XXX' is defined

大神们耽误1min给瞅瞅什么问题,小妹在此谢过了
1.本地远程调用dubbo接口,用xml实例化bean时报No bean named 'projectInterfaces' is defined
2.我本地的xml配置

<?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:jee="http://www.springframework.org/schema/jee"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <context:annotation-config/> 
    <context:component-scan base-package="*"/>    
    <dubbo:application  name="mydubbo"/>    
    <dubbo:registry address="zookeeper://10.11.145.91:2181?backup=10.11.145.100:2181,10.11.145.103:2181" group=""/>   
    <dubbo:protocol name="dubbo" port="-1" serialization="" threads="500"/>
  
    <dubbo:reference id="projectInterfaces" interface="com.abc.trade.projectcenter.interfaces.ProjectInterfaces" check="false" timeout="10000" retries="0" group ="test"/>      
</beans>

3.我的实例化bean代码

public class TestProject {
    public static void main(String[] args) throws Exception {  
        ApplicationContext  context = new ClassPathXmlApplicationContext(  
                new String[]{"classpath*:spring-config-consumer.xml"});

        ProjectInterfaces projectInterfaces = (ProjectInterfaces)context.getBean("projectInterfaces");                                                                    
        
        Message<List<Project>>  msg = projectInterfaces.getProjectsByProductCode("602003162805","200000013631");

        List<Project> m = msg.getData();
        for (Project project : m){
            System.out.println(project.toString());
        }
    }  
}

4.xml中的配置的dubbo:reference id 和interface都是照着其他消费者模块的配置写的(测试环境这些模块都是能正常调用到这个接口的),这个应该没有写错
5.报错是


你的spring-config-consumer.xml文件没有被spring容器加载,所以你注入的这个bean才会在容器里找不到。
context需要调用start方法才行。

ApplicationContext  context = new ClassPathXmlApplicationContext(  
                new String[]{"classpath*:spring-config-consumer.xml"});
context.start();

试试吧


dubbo producer需要在zookeeper中注册才能被cosumer调用,你看一下你这个producer注册没有

【热门文章】
【热门文章】