Spring Integration应用程序在作为打包jar执行时不定义通道

时间:2018-02-11 08:02:34

标签: maven spring-integration

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

但是,当我尝试部署到dev / staging环境时,我遇到了一些与Spring Integration通道定义相关的问题。

经过几个小时完全无能为力(指责外部依赖,我们的开发/暂存环境设置等),我开始意识到每当我尝试将我的应用程序作为打包执行时,我会得到完全相同的问题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)

Bellow你可以找到我的示例应用程序的代码和我用来构建我的打包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

中报告的原因相同

2 个答案:

答案 0 :(得分:0)

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

如果您明确定义频道,该应用是否有效...

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

...

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

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

修改

问题是阴影插件没有合并META-INF/spring.factories文件,所以我们丢失了JAVA DSL的条目,因此不处理任何IntegrationFlow ...

从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通过嵌套jar而不是提取所有类。

<强> 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)。

答案 1 :(得分:0)

加里·罗素(Gary Russell)的回答:问题确实是META-INF/spring.factories文件不会被阴影插件自动合并。

您可以使用阴影插件的AppendingTransformer合并这些文件。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.my.MainClass</mainClass>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.handlers</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.schemas</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.tooling</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.factories</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                </transformers>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>