首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >应用程序在执行打包jar时不定义通道

应用程序在执行打包jar时不定义通道
EN

Stack Overflow用户
提问于 2018-02-11 08:02:33
回答 2查看 1.2K关注 0票数 3

我开始在工作项目中使用。在我的本地dev环境(从Eclipse执行时),一切看起来都很好,运行也很顺利。

然而,当我试图部署到开发/准备环境时,我遇到了一些与的定义相关的问题。

几个小时后,我完全不知所措(归咎于外部依赖关系、我们的开发/暂存环境设置等),当我尝试以打包的jar (在我的本地机器上)执行应用程序时,我开始意识到,当我尝试执行我的应用程序时,我会遇到同样的问题。

为了重现这个问题,我做了一个小的“示例”应用程序,没有任何其他依赖项。在eclipse中,所有操作都很好,但是每次作为打包的jar执行时,都会抛出以下异常:

代码语言:javascript
复制
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。

代码语言:javascript
复制
@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

代码语言:javascript
复制
<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集成引导- intellij在调试工作中,打包jar不”中报道的原因完全相同

EN

回答 2

Stack Overflow用户

发布于 2018-02-11 15:06:50

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

如果你明确定义频道.

代码语言:javascript
复制
@Bean
public MessageChannel gatewayChannel() {
    return new DirectChannel();
}

...?

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

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

编辑

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

从优步罐子里我们只有核心文件..。

代码语言:javascript
复制
org.springframework.integration.config.IntegrationConfigurationInitializer=\
org.springframework.integration.config.GlobalChannelInterceptorInitializer,\
org.springframework.integration.config.IntegrationConverterInitializer,\
org.springframework.integration.config.IdempotentReceiverAutoProxyCreatorInitializer

所以缺少了DSL jar中的附加初始化器..。

代码语言:javascript
复制
org.springframework.integration.config.IntegrationConfigurationInitializer=\
org.springframework.integration.dsl.config.DslIntegrationConfigurationInitializer

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

Spring通过嵌套jars来启动解决这个问题,而不是提取所有类。

EDIT2

下面是另一个解决方案,如果您不能迁移到5,那么将这个bean添加到您的应用程序中.

代码语言:javascript
复制
@Bean
public static BeanFactoryPostProcessor dslInitializer() {
    return new BeanFactoryPostProcessor() {

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

    };
}

(请注意static)。

票数 1
EN

Stack Overflow用户

发布于 2019-09-26 14:06:50

添加到Gary的答案:问题确实是,META-INF/spring.factories文件不是自动合并的阴影插件。

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

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48729539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档