maven - Spring Integration 应用程序在作为打包的 jar 执行时不定义 channel

标签 maven spring-integration

在工作的一个项目中开始使用Spring Integration。在我的本地开发环境中(从 Eclipse 执行时)一切看起来都很好并且运行顺畅。

但是,当我尝试部署到我们的开发/暂存环境时,我遇到了一些与 Spring Integration channel 定义相关的问题。

在完全无能为力的几个小时后(归咎于外部依赖性、我们的开发/暂存环境设置等),我开始意识到每当我尝试将我的应用程序作为一个打包的方式执行时,我都会遇到完全相同的问题jar(在我的本地机器上)

为了重现这个问题,我做了一个没有任何其他依赖项的小型“示例”应用程序。再次从 eclipse 一切正常,但无论何时作为打包的 jar 执行,都会抛出以下异常:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'gatewayChannel' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.integration.support.channel.BeanFactoryChannelResolver.resolveDestination(BeanFactoryChannelResolver.java:89)
    at org.springframework.integration.support.channel.BeanFactoryChannelResolver.resolveDestination(BeanFactoryChannelResolver.java:46)
    at org.springframework.integration.gateway.MessagingGatewaySupport.getRequestChannel(MessagingGatewaySupport.java:344)
    at org.springframework.integration.gateway.MessagingGatewaySupport.send(MessagingGatewaySupport.java:385)
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.invokeGatewayMethod(GatewayProxyFactoryBean.java:481)
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.doInvoke(GatewayProxyFactoryBean.java:433)
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.invoke(GatewayProxyFactoryBean.java:424)
    at org.springframework.integration.gateway.GatewayCompletableFutureProxyFactoryBean.invoke(GatewayCompletableFutureProxyFactoryBean.java:65)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy24.send(Unknown Source)
    at com.test.App$RealApp.doThings(App.java:52)
    at com.test.App.main(App.java:62)

您可以在下面找到我的示例应用程序的代码和我用来构建打包的 jar 的 pom。

@ComponentScan
@EnableIntegration
@IntegrationComponentScan
@Configuration
public class App {

  @MessagingGateway
  public interface GatewayApp {

    @Gateway(requestChannel = "gatewayChannel")
    void send(String string);
  }

  @Bean
  public IntegrationFlow inboud() {
    return IntegrationFlows.from("gatewayChannel")
        .handle(System.out::println)
        .get();
  }

  @Component
  public class RealApp {
    @Autowired
    private GatewayApp gateway;

    public void doThings() {
      gateway.send("yeee");
    }
  }

  @SuppressWarnings("resource")
  public static void main(String[] args) {

    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(App.class);
    context.refresh();
    context.getBean(RealApp.class).doThings();
  }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>sprint-integration</groupId>
    <artifactId>integration</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>integration</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.14.RELEASE</spring.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <createDependencyReducedPom>true</createDependencyReducedPom>
                    <createSourcesJar>false</createSourcesJar>

                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <finalName>yo-service</finalName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>

        <!-- Spring Integration -->
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-amqp</artifactId>
            <version>${spring.version}</version>
        </dependency>

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

注意:我认为这个问题可能与 Spring integration bootstrap - intellij in debug works, packaged jar does not 中报告的原因完全相同

最佳答案

我最好的猜测是 shade 插件对 BeanPostProcessors 的运行顺序有一些影响。

如果您明确定义 channel ,应用程序是否工作...

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

...?

如果是这样,那将是确凿的证据。在这种情况下,下一步是在两种环境中获取 org.springframework 的 DEBUG 日志,并比较 bean 定义/创建/后处理日志。

如果您可以发布一个完整(简单)的示例来展示问题,我们可以看看。

编辑

问题是 shade 插件不合并 META-INF/spring.factories 文件,所以我们丢失了来自 JAVA DSL 的条目,因此不处理任何 IntegrationFlows...

从 Uber jar 中我们只有核心文件...

org.springframework.integration.config.IntegrationConfigurationInitializer=\
org.springframework.integration.config.GlobalChannelInterceptorInitializer,\
org.springframework.integration.config.IntegrationConverterInitializer,\
org.springframework.integration.config.IdempotentReceiverAutoProxyCreatorInitializer

因此缺少 DSL jar 中的附加初始化程序...

org.springframework.integration.config.IntegrationConfigurationInitializer=\
org.springframework.integration.dsl.config.DslIntegrationConfigurationInitializer

因此它适用于 5.0.1,因为 DSL 现在是核心的一部分。

Spring Boot solves this problem通过嵌套 jars 而不是提取所有类。

EDIT2

如果您不能迁移到 Spring Integration 5,这是另一个解决方法。将这个 bean 添加到您的应用程序中......

@Bean
public static BeanFactoryPostProcessor dslInitializer() {
    return new BeanFactoryPostProcessor() {

        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory bf) throws BeansException {
            new DslIntegrationConfigurationInitializer().initialize(bf);
        }

    };
}

(注意 static)。

关于maven - Spring Integration 应用程序在作为打包的 jar 执行时不定义 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48729539/

相关文章:

在 Visio 中使用的 Spring Integration Images

java - Spring 集成 GZIP HTTP 请求

java - Apache Spark 的 generated.java 文件位置

maven - 如何将第 3 方 JAR 添加到我的 Travis-CI maven 构建中?

java - 尝试使用maven程序集插件时出现 "Assembly is incorrectly configured: null"

java - MessageHeaders 在 HttpOutboundGateway 中丢失

java - Maven 找不到依赖项

android - 找不到工具 aapt。请提供正确的 Android SDK 目录路径作为配置参数

java - 参数化 JPA 入站 channel 适配器

java - 如何使用 spring 集成通过 POP3 标记已读消息?