将过滤器从XML迁移到纯Java配置
我正在尝试将一个项目从混合的XML/Java配置迁移到纯Java Config(尚未Java DSL,而是注释@BEAN方法)。 到目前为止,我设法转换了通道,入站通道适配器,变压器和服务激活器),但我坚持使用过滤器的转换。
Integration.xml
文件定义以下过滤器(该消息带有Java.io.file有效载荷)
<int:filter input-channel="channelA" output-channel="channelB"
ref="integrationConfiguration" method="selector"/>
选择器在IntegrationConfiguration类中定义(还持有所有其他与SI相关的@BEAN方法:
@Configuration
public class IntegrationConfiguration {
// channels
@Bean
public MessageChannel channelA() { return new DirectChannel(); }
@Bean
public MessageChannel channelB() { return new DirectChannel(); }
// some other required channels
// ...
// inbound channel adapters
@Bean
@InboundChannelAdapter(channel = "channelA")
public MessageSource<File> fileReadingMessageSource() {
var source = new FileReadingMessageSource();
// source configuration (not relevant here)
return source;
}
// ...
// filter on Message<File>
public boolean selector(@Header("file_name") String name,
@Header("file_relativePath") String relativePath) {
// do stuff with name and relativePath and return true or false
return true;
}
// transformers
@Bean
@Transformer(inputChannel = "channelB", outputChannel = "channelC")
public HeaderEnricher enrichHeaders() {
var expression = new SpelExpressionParser().parseExpression("...");
var headers = Map.of("additional_header",
new ExpressionEvaluatingHeaderValueMessageProcessor<>(expression, String.class));
return new HeaderEnricher(headers);
}
// ...
// service activators
@Bean
@ServiceActivator(inputChannel = "channelC")
public FileWritingMessageHandler fileWritingMessageHandler() {
var handler = new FileWritingMessageHandler(
new SpelExpressionParser().parseExpression("headers.additional_header")
);
// handler configuration (not relevant here)
return handler;
}
// ...
}
I试图用以下方式替换XML定义的bean:
@Bean
@Filter(inputChannel = "channelA", outputChannel = "channelB")
public boolean filter() {
// get the "file_name" and "file_relativePath" headers
var expression1 = new SpelExpressionParser().parseExpression("headers.file_name");
var name = expression1.getValue(String.class);
var expression2 = new SpelExpressionParser().parseExpression("headers.file_relativePath");
String relativePath = expression2.getValue(String.class);
// do stuff with name and relativePath and return true or false
return true;
}
当我运行代码时,它会给我一个 bean> bean creationexception
:
错误创建class路径资源中定义的名称'filter'的bean [.../integrationconfiguration.class.class.class.class ]:通过工厂方法实例化;嵌套的异常是org.springframework.beans.beaninstantiationException:无法实例化[Boolean]:Factory方法“过滤器' null上找不到
我做错了什么?
在
属性或字段“标题” Pojo方法不是必需的,请保留
开箱即用类型:MessageHandler,Transformer,MessagsElector等
在这种情况下,可以使用(不是现成的)@Bean MessageElector,但实际上它是相同结果的更多代码行:
@Bean
@Filter(inputChannel = "channelA", outputChannel = "channelB")
public MessageSelector messageSelector() {
return new MessageSelector(){
@Override
public boolean accept(Message<?>message){
var headers = message.getHeaders();
var name = headers.get("file_name", String.class);
var relativePath = headers.get("file_relativePath", String.class);
return selector(name, relativePath);
}
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有足够的事情可以这样做:
请参阅该
@filter
:@filter
与@serviceactivator
或相似@transformer
:您将方法标记并指向频道。该框架创建了一个端点,并将该方法用作处理程序以从频道消耗消息。方法调用的结果分别为端点目的处理程序。如果过滤器,则请求消息将发送到输出频道(或从标头回复频道),如果结果为true
。否则该消息将被丢弃。在文档中查看更多信息:
There is just enough to do like this:
See docs for that
@Filter
:The
@Filter
is similar to the@ServiceActivator
or@Transformer
: you mark the method and point to the channels. The framework creates an endpoint and use that method as a handler to consume messages from the channel. The result of the method call is handler respectively to the endpoint purpose. In case of filter the request message is sent to the output channel (or reply channel from header) if result istrue
. Otherwise the message is discarded.See more info in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/configuration.html#annotations