Explicit binding creation
在上一部分中,我们解释了如何通过应用程序提供的 Function
、Supplier
或 Consumer
bean 的名称隐式创建 binding。但是,有时可能需要显式创建 binding,其中 binding 不绑定到任何函数。这通常是通过 StreamBridge
与其他框架进行集成来完成的。
In the previous section we explained how bindings are created implicitly driven by the names of Function
, Supplier
or Consumer
beans provided by your application.
However, there are times when you may need to create binding explicitly where bindings are not tied to any function. This is typically done to
support integrations with other frameworks via StreamBridge
.
Spring Cloud Stream 允许通过 spring.cloud.stream.input-bindings
和 spring.cloud.stream.output-bindings
属性显式定义输入和输出绑定。请注意,属性名称中复数形式允许使用 ;
来分隔符来定义多个绑定。下面是一个测试用例作为示例:
Spring Cloud Stream allows you to define input and output bindings explicitly via spring.cloud.stream.input-bindings
and spring.cloud.stream.output-bindings
properties. Noticed the plural in the property names allowing you to define multiple bindings by simply using ;
as a delimiter.
Just look at the following test case as an example:
@Test public void testExplicitBindings() { try (ConfigurableApplicationContext context = new SpringApplicationBuilder( TestChannelBinderConfiguration.getCompleteConfiguration(EmptyConfiguration.class)) .web(WebApplicationType.NONE) .run("--spring.jmx.enabled=false", "--spring.cloud.stream.input-bindings=fooin;barin", "--spring.cloud.stream.output-bindings=fooout;barout")) { . . . } } @EnableAutoConfiguration @Configuration public static class EmptyConfiguration { }
如你所见,我们声明了两个输入绑定和两个输出绑定,而我们的配置没有定义任何函数,但我们已能够成功创建这些绑定并访问其对应的频道。
As you can see we have declared two input bindings and two output bindings while our configuration had no functions defined, yet we were able to successfully create these bindings and access their corresponding channels.