Spring 集成从 5.2.x 升级到 5.3 的问题

Spring Integration upgrade from 5.2.x to 5.3 problem

我想将 Spring 引导版本从 2.2.7.RELEASE 升级到 2.3.0.RELEASE,我也在使用 Spring 集成文件和 XML配置(见下文)。 启动我的应用程序时,抛出 NullPointerException。调试问题后,根本原因是 bean file:inbound-channel-adapter 的创建。有趣的是,只有这种类型的 bean 会导致此问题,而所有其他 bean 都不会。 有人遇到过类似的问题吗?任何人都可以帮助将此 XML 转换为 Java 配置样式?提前致谢。

<?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:int="http://www.springframework.org/schema/integration"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:file="http://www.springframework.org/schema/integration/file"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
       http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">


    <bean id="compositeFilter" class="org.springframework.integration.file.filters.CompositeFileListFilter">
        <constructor-arg>
            <list>
                <ref bean="ignoreHiddenFileListFilter"/>
                <ref bean="acceptOnceFileListFilter"/>
                <ref bean="extensionFileFilter"/>
            </list>
        </constructor-arg>
    </bean>

    <!-- upload folder listener  -->
    <file:inbound-channel-adapter id="uploadFilesScanner" directory="${integration.upload-folder}" filter="compositeFilter">
        <int:poller fixed-rate="${integration.poller-rate:10000}"/>
    </file:inbound-channel-adapter>

    <file:outbound-gateway request-channel="uploadFilesScanner" reply-channel="batchFilesScanner" directory="${integration.directory}" delete-source-files="true"/>

    <!-- 1) Scan for files -->
    <file:inbound-channel-adapter id="batchFilesScanner" directory="${integration.directory}" filter="compositeFilter">
        <int:poller fixed-rate="5000"/>
    </file:inbound-channel-adapter>

    <!-- 2) move the file to processing -->
    <file:outbound-gateway request-channel="batchFilesScanner" reply-channel="batchFilesProcessing" directory="${integration.processing-folder}" delete-source-files="true"/>

    <!-- 3) transform csv file -->
    <int:service-activator input-channel="batchFilesProcessing" output-channel="batchFilesTran" ref="batchTransformerTask" method="execute"/>

    <!-- 4) move the file to archive folder -->
    <file:outbound-gateway request-channel="batchFilesTran" reply-channel="batchFilesArchive" directory="${integration.archive-folder}" delete-source-files="true"/>

    <int:service-activator input-channel="batchFilesArchive" ref="cleanupHelper" method="execute"/>

    <!-- Transformer task channel configuration -->
    <task:executor id="batchFilesTranTaskExecutor" pool-size="1" rejection-policy="CALLER_RUNS" queue-capacity="1"/>
    <int:channel id="batchFilesTran">
        <int:dispatcher load-balancer="round-robin" task-executor="batchFilesTranTaskExecutor" failover="false"/>
    </int:channel>

    <int:service-activator input-channel="errorChannel" ref="intErrorHandler" method="execute"/>

</beans>

NullPointerException:

Caused by:
        java.lang.NullPointerException
            at org.springframework.integration.support.utils.IntegrationUtils.obtainComponentName(IntegrationUtils.java:205)
            at org.springframework.integration.graph.IntegrationGraphServer$NodeFactory.sourceNode(IntegrationGraphServer.java:399)
            at org.springframework.integration.graph.IntegrationGraphServer.lambda$pollingAdapters(IntegrationGraphServer.java:216)
            at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:195)
            at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
            at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
            at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
            at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
            at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
            at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
            at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
            at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
            at org.springframework.integration.graph.IntegrationGraphServer.pollingAdapters(IntegrationGraphServer.java:221)
            at org.springframework.integration.graph.IntegrationGraphServer.buildGraph(IntegrationGraphServer.java:184)
            at org.springframework.integration.graph.IntegrationGraphServer.onApplicationEvent(IntegrationGraphServer.java:116)
            at org.springframework.integration.graph.IntegrationGraphServer.onApplicationEvent(IntegrationGraphServer.java:67)
            at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
            at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
            at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
            at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:403)
            at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:360)
            at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:897)
            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553)
            at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
            at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
            at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
            at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
            at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120)
            at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
            at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
            ... 88 more

问题已确认。 FileReadingMessageSourceFactoryBean 不会将 beanName 填充到它生成的 FileReadingMessageSource 中。

您的配置太大,无法立即转换到这里。考虑遵循文档:https://docs.spring.io/spring-integration/docs/current/reference/html/overview.html#programming-tips

Java 和注释配置模拟 <file:inbound-channel-adapter> 是这样的:

@Bean
@InboundChannelAdapter
public FileReadingMessageSource fileReadingMessageSource() {

...
}

在此处查看更多信息:https://docs.spring.io/spring-integration/docs/current/reference/html/configuration.html#annotations

使用 Java DSL 只需选择一个 IntegrationFlows.from(MessageSource) 工厂并跟随其构建者完成其余工作:

https://docs.spring.io/spring-integration/docs/current/reference/html/dsl.html#java-dsl-inbound-adapters

我坚持使用 Spring 引导版本 2.3.0,并且只将 SI 版本覆盖到 5.3.1 版本并且它有效。异常消失了。