代码之家  ›  专栏  ›  技术社区  ›  Payal Bansal

使用Spring集成将文件复制到另一个引发异常的目录的基本示例

  •  1
  • Payal Bansal  · 技术社区  · 6 年前

    我是春季一体化的初学者。我在Spring引导中编写了这段代码,它引发了异常:“名为'messagesource'的bean应该是'org.springframework.context.messagesource'类型,但实际上是'org.springframework.integration.file.filereadingmessagesource'类型。”

    代码:

    @Configuration
    /*@EnableIntegration annotation designates this class as a Spring Integration configuration.*/
    @EnableIntegration
    public class SIConfig {
    
        @Bean
        public MessageChannel channel() {
            return new DirectChannel();
        }
        //bydefault name of method
        @Bean
        public MessageSource messageSource() {
            FileReadingMessageSource ms= new FileReadingMessageSource();
            ms.setDirectory(new File("C:\\Users\\payal\\Pictures"));
            ms.setFilter(new SimplePatternFileListFilter("*.mp4"));
            return ms;
        }
    
        @Bean
        public MessageHandler handler() {
            FileWritingMessageHandler handler= new FileWritingMessageHandler(new File("C:\\Users\\payal\\Documents\\batch7"));
            handler.setFileExistsMode(FileExistsMode.IGNORE);
            handler.setExpectReply(false);
            return handler;
    
        }
    
        @Bean
        public IntegrationFlow flow() {
            return IntegrationFlows.from(messageSource(),  configurer -> configurer.poller(Pollers.fixedDelay(10000)))
                    .channel(channel())
                    .handle(handler()).get();
        }
    
    }
    

    由于使用boot,版本将自动管理

    上传代码n github也:

    https://github.com/LearningNewTechnology/SpringIntegrationOne

    任何帮助都将不胜感激。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Gary Russell    6 年前

    将bean的名称更改为,例如

    @Bean
    public MessageSource myMessageSource() {
    

    Spring框架(上下文)具有另一种类型的 MessageSource Spring引导自动配置创建了一个名为 messageSource 所以你的豆子和那个碰撞了。