存储库接口的实例通常由容器创建,在使用 Spring Data 时,Spring 是最自然的选择。
Spring Data LDAP 包含一个自定义 CDI 扩展,允许您在 CDI 环境中使用存储库抽象。
扩展是 JAR 的一部分。
要激活它,请将Spring Data LDAP JAR拖放到类路径中。
现在,您可以通过为 实现 的 CDI 生产者来设置基础架构,如以下示例所示:LdapTemplate
class LdapTemplateProducer {
@Produces
@ApplicationScoped
public LdapOperations createLdapTemplate() {
ContextSource contextSource = …
return new LdapTemplate(contextSource);
}
}
每当容器请求存储库类型的 Bean 时,Spring Data LDAP CDI 扩展都会选取 CDI bean,并为 Spring Data 存储库创建代理。
因此,获取 Spring Data 存储库的实例只需声明注入的属性即可,如以下示例所示:LdapTemplate
class RepositoryClient {
@Inject
PersonRepository repository;
public void businessMethod() {
List<Person> people = repository.findAll();
}
}