使用CDI

Spring Data Elasticsearch repositories也可以使用CDI注入。

Example 36. Spring Data Elasticsearch repositories using JavaConfig(使用JavaConfig)

  1. class ElasticsearchTemplateProducer {
  2. @Produces
  3. @ApplicationScoped
  4. public ElasticsearchOperations createElasticsearchTemplate() {
  5. return new ElasticsearchTemplate(nodeBuilder().local(true).node().client());
  6. }
  7. }
  8. class ProductService {
  9. private ProductRepository repository;
  10. public Page<Product> findAvailableBookByName(String name, Pageable pageable) {
  11. return repository.findByAvailableTrueAndNameStartingWith(name, pageable);
  12. }
  13. @Inject
  14. public void setRepository(ProductRepository repository) {
  15. this.repository = repository;
  16. }
  17. }