【问题标题】:Spring Integration "No bean named 'toFtpChannel' available"Spring 集成“没有名为 'toFtpChannel' 的 bean 可用”
【发布时间】:2019-03-16 20:47:33
【问题描述】:

我需要上传一张图片到 FTP 服务器。所以我创建了与 SessionFactory、MessageHandler 和 MessageGateway 的集成配置,用于将文件上传到 FTP 服务器:

@Configuration
@IntegrationComponentScan
public class FtpConfiguration {

    @Bean
    public SessionFactory<FTPFile> ftpSessionFactory() {
        DefaultFtpSessionFactory defaultFtpSessionFactory = new DefaultFtpSessionFactory();
        //setup
        return new CachingSessionFactory<>(defaultFtpSessionFactory);
    }

    @Bean
    @ServiceActivator(inputChannel = "toFtpChannel")
    public MessageHandler handler() {
        FtpMessageHandler handler = new FtpMessageHandler(ftpSessionFactory());
        handler.setAutoCreateDirectory(true);
        handler.setRemoteDirectoryExpression(new LiteralExpression(""));
        handler.setFileNameGenerator(message -> (String) message.getHeaders().get("filename"));
        return handler;
    }

    //to show you that I've tried both
    /*@Bean
    public IntegrationFlow ftpOutboundFlow() {
        return IntegrationFlows.from("toFtpChannel")
                .handle(Ftp.outboundAdapter(ftpSessionFactory(), FileExistsMode.REPLACE)
                        .useTemporaryFileName(false)
                        .fileNameExpression("headers['" + FileHeaders.FILENAME + "']")
                        .remoteDirectory("")
                ).get();
    }*/

    @MessagingGateway
    public interface UploadGateway {

        @Gateway(requestChannel = "toFtpChannel")
        void upload(@Payload byte[] file, @Header("filename") String filename, @Header("path") String path);
    }

}

成功构建应用程序。然后我正在尝试上传一些文件:

@Autowired
UploadGateway uploadGateway;


@Override
public void uploadImage(byte[] scanBytes, String filename, String path) {
    try {
        uploadGateway.upload(scanBytes, filename, path);
    } catch (Exception e) {
        log.error("WRONG", e);
    }
}

然后它说:“没有名为 'toFtpChannel' 的 bean 可用” 我几乎尝试了所有教程,我做错了什么?

依赖:

 <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-file</artifactId>
        <version>RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-integration</artifactId>
        <version>RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-ftp</artifactId>
        <version>RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-java-dsl</artifactId>
        <version>RELEASE</version>
    </dependency>

【问题讨论】:

    标签: java spring ftp spring-integration


    【解决方案1】:

    在您可以在注释中引用它之前,请求通道应该实际存在。声明频道,该问题应该消失了

    @Bean
    public MessageChannel toFtpChannel() {
        return new DirectChannel();
    }
    

    此示例创建DirectChannel,但您可以根据您的语义选择any implementation

    希望对你有帮助!

    【讨论】:

    • 查看我的答案以了解更明显的问题原因。
    【解决方案2】:

    您的应用程序看起来不像是真正的 Spring Boot:我们没有看到 @SpringBootApplication 注释。正是这一点将启动正确的自动配置,包括用于 Spring 集成的一个。

    如果您仍然不关心那里的 Spring Boot,那么您缺少标准的 @EnableIntegration 注释:https://docs.spring.io/spring-integration/docs/current/reference/html/overview.html#configuration-enable-integration

    【讨论】:

      【解决方案3】:

      @Sergey Prokofiev 的答案在这里解决了,但我想指出一件奇怪的事情。

      我有一个
      @Gateway(requestChannel = "json.event.deserialize.input")
      并且下面的 bean 不起作用:

          @Bean
          public MessageChannel jsonEventDeserializeInput() {
              return MessageChannels.direct("json.event.deserialize.input").get();
          }
      

      在我将网关更改为
      @Gateway(requestChannel = "jsonEventDeserializeInput")
      它确实有效。

      因此,即使文档说 id(...)Used as the beanName to register the bean in the application context for this component.,它也没有找到任何名称等于我传递给 id(...) 的值的 bean。

      【讨论】:

        猜你喜欢
        • 2018-01-05
        • 2020-12-02
        • 2021-07-08
        • 2018-10-14
        • 2021-03-18
        • 2017-12-16
        • 1970-01-01
        • 2018-02-21
        • 2020-03-21
        相关资源
        最近更新 更多