【问题标题】:Spring Integrtion XML and Java Config ConversionSpring 集成 XML 和 Java 配置转换
【发布时间】:2018-11-06 22:25:38
【问题描述】:

我是 Spring Integration 的新手,我的项目正在使用文件支持来读取文件并加载到数据库中。

我有 XML 配置,试图理解它的内容。

<int-file:inbound-channel-adapter auto-startup= true channel="channelOne" directory="${xx}" filename-regex="${xx}" id="id" prevent-duplicates="false">
        <int:poller fixed-delay="1000" receive-timeout="5000"/>
</int-file:inbound-channel-adapter>        

<int:channel id="channelOne"/>

从上面那篇文章,我的理解是:

  1. 我们定义了一个通道和
  2. 然后定义 inbound-channel-adapter - 这将查看文件的目录并创建一条以文件为有效负载的消息。

我可以在 JavaConfig 中进行如下转换:

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

@Bean
@InboundChannelAdapter(value = "fileInputChannel", poller = @Poller(fixedDelay = "1000"))
    public MessageSource<File> fileReadingMessageSource() {
        FileReadingMessageSource sourceReader= new FileReadingMessageSource();
        RegexPatternFileListFilter regexPatternFileListFilter = new RegexPatternFileListFilter(
                file-regex);

        //List<FileListFilter<File>> fileListFilter = new ArrayList<FileListFilter<File>>();
        fileListFilter.add(regexPatternFileListFilter);
        //CompositeFileListFilter compositeFileListFilter = new CompositeFileListFilter<File>(
                fileListFilter);
        sourceReader.setDirectory(new File(inputDirectorywhereFileComes));
        sourceReader.setFilter(regexPatternFileListFilter );
        return sourceReader;
    }

然后是下一段代码,实际上我很难理解,而且要转换为 JavaConfig。

这是下一段:

<int-file:outbound-gateway
        delete-source-files="true"
        directory="file:${pp}"
        id="id"
        reply-channel="channelTwo"
        request-channel="channelOne"
        temporary-file-suffix=".tmp"/>

<int:channel id="channelTwo"/>
<int:outbound-channel-adapter channel="channelTwo" id="id" method="load" ref="beanClass"/>

所以从这篇文章,我的理解:

1:定义一个输出通道。 2:定义一个出站网关,它将将该消息作为文件再次写入目录(另一个),同时从源目录中删除文件。最后会调用Bean Class的Load方法。这是我们的类,并且有 load 方法,它将文件作为输入并将其加载到 DB。

我试图将其转换为 Java Config。这是我的代码:

 @Bean
    @ServiceActivator(inputChannel= "fileInputChannel")
    public MessageHandler fileWritingMessageHandler() throws IOException, ParseException {
        FileWritingMessageHandler handler = new FileWritingMessageHandler(new File(path to output directory));
        handler.setFileExistsMode(FileExistsMode.REPLACE);

    beaObject.load(new File(path to output directory or input directory:: Nothing Worked));
        handler.setDeleteSourceFiles(true);
        handler.setOutputChannel(fileOutputChannel());
        return handler;
    }

我可以将此文件写入输出文件夹,也可以从源中删除。在那之后,我完全迷失了。我必须调用我的 BeanClass 的方法 Load(ref=class in XML )。

我尝试了很多,但无法得到它。多次阅读集成文件支持文档,但无法完成。

注意:当我尝试时,我收到一个错误,即“找不到文件异常”。我相信,我可以调用我的方法,但无法获取文件。

此 XML 配置运行良好。

如果可能的话,任何人都可以提出与 DSL 的 Spring 集成。

请帮助我了解基本流程并完成这件事。任何帮助和 cmets 都是非常可观的。

提前致谢。

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

    首先,您需要了解@Bean 方法完全适用于稍后将在运行时使用的配置和组件定义。您绝对不能在@Bean 中调用业务逻辑。我的意思是你的beaObject.load() 是完全错误的。

    所以,请先去 Spring Framework Docs 了解什么是 @Bean 及其父级 @Configurationhttps://docs.spring.io/spring/docs/5.1.2.RELEASE/spring-framework-reference/core.html#beans-java

    FileWritingMessageHandler@ServiceActivator 确实正确(当您删除 beaObject.load() 时)。您只需要再声明一个@ServiceActivator,以便在fileOutputChannel 中出现消息时在运行时调用您的beaObject.load()

    @ServiceActivator(inputChannel= "fileOutputChannel")
    public void loadFileIntoDb(File payload) {
           this.beaObject.load(payload);
    }
    

    请参阅https://docs.spring.io/spring-integration/docs/5.1.1.BUILD-SNAPSHOT/reference/html/configuration.html#annotations 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-08
      • 2015-05-30
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 2015-07-22
      相关资源
      最近更新 更多