跳到主要内容

CDI 集成

QWen Flash 中英对照 CDI Integration

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

class LdapTemplateProducer {

@Produces
@ApplicationScoped
public LdapOperations createLdapTemplate() {

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

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

class RepositoryClient {

@Inject
PersonRepository repository;

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