Spring Integration DSL官方参考文档代码示例无法编译该如何解决
解决方案
报错的核心原因是方法引用语法使用不规范:integerSource是配置类中定义的Bean方法名,不是实例对象,不能直接作为方法引用的左侧调用实例方法getAndIncrement。
方案1:通过方法参数注入Bean(推荐)
将AtomicInteger实例注入到myFlow的Bean方法参数中,直接使用注入的实例构造方法引用,符合Spring依赖注入规范。
@Configuration @EnableIntegration public class MyConfiguration { @Bean public AtomicInteger integerSource() { return new AtomicInteger(); } // 参数注入上方定义的AtomicInteger实例 @Bean public IntegrationFlow myFlow(AtomicInteger integerSource) { return IntegrationFlows.from(integerSource::getAndIncrement, c -> c.poller(Pollers.fixedRate(100))) .channel("inputChannel") .filter((Integer p) -> p > 0) .transform(Object::toString) .channel(MessageChannels.queue()) .get(); } }
方案2:直接调用Bean方法获取实例
利用@Configuration类的代理特性,直接调用integerSource()方法获取Spring管理的单例实例,再构造方法引用。
@Configuration @EnableIntegration public class MyConfiguration { @Bean public AtomicInteger integerSource() { return new AtomicInteger(); } @Bean public IntegrationFlow myFlow() { // 调用integerSource()获取Spring管理的实例后构造方法引用 return IntegrationFlows.from(integerSource()::getAndIncrement, c -> c.poller(Pollers.fixedRate(100))) .channel("inputChannel") .filter((Integer p) -> p > 0) .transform(Object::toString) .channel(MessageChannels.queue()) .get(); } }
额外检查项
- 确认项目已引入正确的Spring Integration依赖,Spring Boot项目直接引入官方starter即可:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId> </dependency> - 若IDEA仅静态检查报错、实际编译运行正常,刷新项目依赖并重建IDEA索引即可消除误报。
- 上述写法兼容Spring Integration 5.x、6.x主流版本,若仍有API报错可核对版本与官方文档匹配度。
内容的提问来源于stack exchange,提问作者Chris
相关产品推荐
相关产品推荐

