Native Images 支持

从 6.0 版本开始,Spring Integration 应用程序使用 GraalVM 编译为 Native Images 的功能已通过 Spring AOT native hints 得到支持。对于大多数常见用例,例如使用 @Bean 方法定义端点、使用 lambda 的 Java DSL 配置以及 @MessagingGateway 接口扫描(导入),框架提供了相应的反射、代理和序列化提示。如果配置在 POJO 方法上使用了消息注解(@ServiceActivator@Splitter 等),或者 POJO 方法与 IntegrationFlowBuilder.handle(Object service, String methodName) API 一起使用,则这些方法也必须标记 @Reflective 注解,因为它们是由框架通过反射调用的。

Native Images 不支持 XML 配置。

如前所述,带有 @MessagingGateway 注解的服务接口在被 @IntegrationComponentScan 扫描或在 @Import 注解中使用时,会由框架处理并将相应的代理提示暴露到 AOT contribution 中。当使用 IntegrationFlow.from(Class<?> serviceInterface) API 声明网关时,必须手动暴露为此类接口配置的代理

@Configuration
@EnableIntegration
@ImportRuntimeHints(GatewayRuntimeHints.class)
public class IntegrationConfiguration {

    @Bean
    IntegrationFlow someFlow() {
        return IntegrationFlow.from(SomeGateway)
                  // ...
                   .get();
    }

    public interface SomeGateway {

        void doSomething(Object payload);

    }

    private static class GatewayRuntimeHints implements RuntimeHintsRegistrar {

        @Override
        public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
            hints.proxies().registerJdkProxy(
                                   AopProxyUtils.completeJdkProxyInterfaces(SomeGateway));
        }

    }

}
IntegrationFlow 的内容在 AOT 处理阶段不会被处理。因此,一些提示(例如上面提到的网关代理提示)必须由目标应用程序手动提供。

当然,配置只是集成解决方案的一部分。最重要的部分是数据在网络上的传输以及持久化存储。这正是序列化在许多用例中派上用场的地方。Spring Integration 为框架内部使用的以下类型向 native image 配置暴露了序列化提示:StringNumberLongDateArrayListHashMapPropertiesHashtableExceptionUUIDGenericMessageErrorMessageMessageHeadersAdviceMessageMutableMessageMutableMessageHeadersMessageGroupMetadataMessageHolderMessageMetadataMessageHistoryMessageHistory.EntryDelayHandler.DelayedMessageWrapper。对于用户特定的数据(主要以消息 payload 的形式存在),必须通过 RuntimeHintsRegistrar 实现手动暴露序列化提示,正如上面网关代理所示的那样,并使用相应的 RuntimeHints.serialization().registerType() API。

建议使用 Spring Boot 及其相应的构建工具开发 native integration 应用程序。