美文网首页
跟着尚硅谷学习spring cloud Stream 启动880

跟着尚硅谷学习spring cloud Stream 启动880

作者: 进击的奥莉 | 来源:发表于2021-09-07 10:46 被阅读0次

    报错内容

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendMessageController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IMessageProviderServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.messaging.MessageChannel' available: expected single matching bean but found 2: nullChannel,errorChannel
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:337) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    

    大致意思是找不到依赖...
    在application.yml文件中配置如下:

    bindings:
            output:
              destination: studyExchange
              content-type: application/json
              default-binder: defaultRabbit  # 如果是binder,defaultRabbit  会变红
    

    service层代码

    public interface IMessageProviderService {
        String send();
    }
    

    实现类impl代码,
    我的错误是Source引入错了包,导致一直报错:expected single matching bean but found 2: nullChannel,errorChannel
    错误包:import javax.xml.transform.Source;
    正确包:import org.springframework.cloud.stream.messaging.Source;

    package com.yanheng.springcloud.service.impl;
    
    import com.yanheng.springcloud.service.IMessageProviderService;
    import org.springframework.cloud.stream.annotation.EnableBinding;
    import org.springframework.messaging.MessageChannel;
    import org.springframework.messaging.support.MessageBuilder;
    import org.springframework.cloud.stream.messaging.Source; // 正确包
    
    import javax.annotation.Resource;
    import java.util.UUID;
    
    @EnableBinding(Source.class) // 定义消息的推送通道
    public class IMessageProviderServiceImpl implements IMessageProviderService {
        @Resource
        private MessageChannel output; // 消息发送通道
    
        @Override
        public String send() {
            String serial = UUID.randomUUID().toString();
            output.send(MessageBuilder.withPayload(serial).build());
            System.out.println("********serial: " + serial);
            return serial;
        }
    }
    

    controller层代码

    @RestController
    public class sendMessageController {
        @Resource
        private IMessageProviderService iMessageProviderService;
    
        @GetMapping(value = "/sendMsg")
        public String sendMessage() {
            return iMessageProviderService.send();
        }
    }
    

    找了两天也没在网络上找到正确的解决办法,有的说加@Compent,或者@Qualifier等注解,都是不可行的。希望能帮助需要的人。

    相关文章

      网友评论

          本文标题:跟着尚硅谷学习spring cloud Stream 启动880

          本文链接:https://www.haomeiwen.com/subject/hwbuwltx.html