尝试注入 ExampleMatcher 时出错

Error while trying to inject ExampleMatcher

我正在使用 Query by Example 来实现一些 我的 Spring 引导应用程序中的复杂过滤器。

我使用 ExampleMatcher 来定义必须如何处理字符串属性。所以我在几个需要过滤器的不同控制器中有一个类似于下面的代码:

  @GetMapping("/municipio/filter")
  public @ResponseBody ResponseEntity<?> filtro(
    Municipio municipio,
    Pageable page,
    PagedResourcesAssembler<Municipio> assembler
  ){

    ExampleMatcher matcher = ExampleMatcher.matching()
        .withIgnoreCase()
        .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);

    Example example = Example.of(municipio, matcher);
    Page<Municipio> municipios = this.municipioRepository.findAll(example, page);
    return ResponseEntity.ok(assembler.toResource(municipios));

  }

我想以集中方式定义 ExampleMatcher,以避免在每个控制器中复制此配置。所以我尝试将其定义为 Bean 并以这种方式注入:

@Configuration
public class AppConfig {

  @Bean
  public ExampleMatcher getExampleMatcher() {
    return ExampleMatcher.matching()
        .withIgnoreCase()
        .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
  }
}

//Withing the controller class
@GetMapping("/municipio/filter")
public @ResponseBody ResponseEntity<?> filtro(
    Municipio municipio,
    Pageable page,
    PagedResourcesAssembler<Municipio> assembler,
    ExampleMatcher matcher //ExampleMatcher should be injected by Spring
  ){

    Example example = Example.of(municipio, matcher);
    Page<Municipio> municipios = this.municipioRepository.findAll(example, page);
    return ResponseEntity.ok(assembler.toResource(municipios));

  }

但是我得到以下错误:

[No primary or default constructor found for interface org.springframework.data.domain.ExampleMatcher]: java.lang.IllegalStateException: No primary or default constructor found for interface org.springframework.data.domain.ExampleMatcher

谁能指出我在这里遗漏了什么?

ExampleMatcher 匹配器 不会作为您的控制器方法的参数注入,请参阅下面的代码来了解如何定义它:

@Controller// or @RestController
public class SomeController {
    @Autowired
    ExampleMatcher matcher;
    // rest of the code ...
}

并将其从参数列表中删除