Spring Batch中RepositoryItemReader传List参数匹配不到方法如何解决
RepositoryItemReader参数匹配异常解决方案
根因分析
RepositoryItemReader的setArguments方法接收的列表是方法的参数列表,列表里的每一个元素对应目标方法的一个入参。你直接传入包含2个枚举值的listaStatusCadOnline时,Spring Batch会把列表里的两个枚举值拆成两个独立的入参,再加上自动拼接的分页参数Pageable,最终要调用的方法签名变成了findByFlCadastroOnlineIn(StatusCadOnline, StatusCadOnline, Pageable),和你Repository中定义的findByFlCadastroOnlineIn(List<StatusCadOnline>, Pageable)不匹配,所以抛出NoSuchMethodException。
修复方案
你只需要把要传入的整个List<StatusCadOnline>作为参数列表的第一个元素,再传给setArguments即可,修改reader配置中的参数设置行:
// 原错误写法 // reader.setArguments(listaStatusCadOnline); // 修正后写法:把整个List包装为参数列表的第一个元素 reader.setArguments(Collections.singletonList(listaStatusCadOnline)); // JDK 9+也可以用 List.of(listaStatusCadOnline)
修改后,Spring Batch拿到的参数列表只有1个元素(就是你定义的状态枚举列表),调用Repository方法时会传入这个List作为第一个参数,再拼接自动生成的Pageable作为第二个参数,完全匹配你定义的Repository方法签名,问题即可解决。
不推荐的替代方案:可以修改Repository方法签名为接收多个独立的枚举参数,如下:
Page<Interlocutor> findByFlCadastroOnlineIn(StatusCadOnline flCadastroOnline1, StatusCadOnline flCadastroOnline2, Pageable pageable);该方案扩展性极差,新增查询状态需要同步修改方法定义,仅做参考。
内容的提问来源于stack exchange,提问作者Paulo Hirata
相关产品推荐
相关产品推荐

