5.1.3 使用CDI

优质
小牛编辑
130浏览
2023-12-01

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

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

class ElasticsearchTemplateProducer {

    @Produces
    @ApplicationScoped
    public ElasticsearchOperations createElasticsearchTemplate() {
        return new ElasticsearchTemplate(nodeBuilder().local(true).node().client());
    }
}

class ProductService {

    private ProductRepository repository;

    public Page<Product> findAvailableBookByName(String name, Pageable pageable) {
        return repository.findByAvailableTrueAndNameStartingWith(name, pageable);
    }

    @Inject
    public void setRepository(ProductRepository repository) {
        this.repository = repository;
    }
}