我开始在工作中的项目中使用春季集成。一切看起来都很好,并且在我的本地开发环境上(从Eclipse执行时(。
但是,当我尝试部署到我们的开发/分期环境时,我会发现一些与春季集成渠道的定义有关的问题。
几个小时完全无知(指责外部依赖项,我们的开发/登台环境设置等(,我意识到,每当我试图执行我的应用程序作为打包时,我都会遇到完全相同的问题jar (在我的本地计算机上(
我做了一个小的"示例"应用程序,而没有任何其他依赖项来重现此问题。Eclipse再次有效,但是每当被执行为包装罐时,都会抛出以下例外:
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)
波纹管您可以找到我的示例应用程序的代码以及我用来构建包装罐的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>
注意:我认为这个问题可能与春季集成bootstrap中报道的原因相同的原因 - Intellij在调试作品中,包装罐不
我最好的猜测是阴影插件对运行BeanPostProcessors
的顺序有一定影响。
如果您明确定义了频道...
是否有效@Bean
public MessageChannel gatewayChannel() {
return new DirectChannel();
}
...?
如果是这样,那将是一把吸烟。在这种情况下,下一步将是在两个环境中获取org.springframework
的调试日志,并比较BEAN定义/创建/后处理日志。
如果您可以发布一个显示问题的完整(简单(示例,我们可以看看。
编辑
问题是阴影插件不合并META-INF/spring.factories
文件,因此我们从Java DSL中丢失了条目,因此不会处理任何IntegrationFlow
S ...
在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通过嵌套罐子而不是提取所有类来解决此问题。
edit2
这是另一个工作,如果您不能移动春季集成5。将此bean添加到您的应用程序...
@Bean
public static BeanFactoryPostProcessor dslInitializer() {
return new BeanFactoryPostProcessor() {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory bf) throws BeansException {
new DslIntegrationConfigurationInitializer().initialize(bf);
}
};
}
(注意static
(。
添加到加里·罗素(Gary Russell(的答案中:问题确实在于, META-INF/spring.factories
文件不是由阴影插件自动合并的。
您可以使用Shade插件的附录TransFormer合并这些文件。
<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>