CDI Integration

仓库界面的实例通常由容器创建,在使用 Spring Data 时,Spring 是最自然的选择。Spring Data LDAP 包含一个自定义 CDI 扩展,可让你在 CDI 环境中使用仓库抽象。该扩展是 JAR 的一部分。要激活它,请将 Spring Data LDAP JAR 放入你的类路径中。你现在可以通过实施 LdapTemplate 的 CDI Producer 设定基础设施,如下例所示:

Instances of the repository interfaces are usually created by a container, for which Spring is the most natural choice when working with Spring Data. Spring Data LDAP includes a custom CDI extension that lets you use the repository abstraction in CDI environments. The extension is part of the JAR. To activate it, drop the Spring Data LDAP JAR into your classpath. You can now set up the infrastructure by implementing a CDI Producer for the LdapTemplate, as the following example shows:

class LdapTemplateProducer {

    @Produces
    @ApplicationScoped
    public LdapOperations createLdapTemplate() {

        ContextSource contextSource = …
        return new LdapTemplate(contextSource);
    }
}

Spring Data LDAP CDI 扩展将 LdapTemplate 作为 CDI bean,并在容器请求仓库类型的 bean 时为 Spring Data 仓库创建一个代理。因此,获取 Spring Data 仓库的实例是声明一个已注入属性的问题,如下例所示:

The Spring Data LDAP CDI extension picks up the LdapTemplate as a CDI bean and creates a proxy for a Spring Data repository whenever a bean of a repository type is requested by the container. Thus, obtaining an instance of a Spring Data repository is a matter of declaring an injected property, as the following example shows:

class RepositoryClient {

  @Inject
  PersonRepository repository;

  public void businessMethod() {
    List<Person> people = repository.findAll();
  }
}