|
对于最新的稳定版本,请使用 Spring Data LDAP 4.0.4! |
用法
要访问存储在符合 LDAP 标准的目录中的领域实体,您可以使用我们强大的仓库支持,这将大大简化实现过程。 为此,请为您的仓库创建一个接口,如下例所示:
示例 1. 示例 Person 实体
@Entry(objectClasses = { "person", "top" }, base="ou=someOu")
public class Person {
@Id
private Name dn;
@Attribute(name="cn")
@DnAttribute(value="cn", index=1)
private String fullName;
@Attribute(name="firstName")
private String firstName;
// No @Attribute annotation means this is bound to the LDAP attribute
// with the same value
private String firstName;
@DnAttribute(value="ou", index=0)
@Transient
private String company;
@Transient
private String someUnmappedField;
// ...more attributes below
}
我们这里有一个简单的领域对象。
请注意,它有一个名为 dn 的属性,类型为 Name。
有了该领域对象,我们可以通过为其定义一个接口来创建一个用于持久化该类型对象的仓库,如下所示:
示例 2. 用于持久化
Person 实体的基本仓库接口public interface PersonRepository extends CrudRepository<Person, Long> {
// additional custom finder methods go here
}
由于我们的领域仓库扩展了 CrudRepository,它为您提供了 CRUD 操作以及访问实体的方法。
使用仓库实例只需通过依赖注入将其注入到客户端即可。
示例 3. 访问 Person 实体
@ExtendWith(SpringExtension.class)
@ContextConfiguration
class PersonRepositoryTests {
@Autowired PersonRepository repository;
@Test
void readAll() {
List<Person> persons = repository.findAll();
assertThat(persons.isEmpty(), is(false));
}
}
该示例使用 Spring 的单元测试支持创建一个应用上下文,它将对测试用例执行基于注解的依赖注入。 在测试方法内部,我们使用仓库来查询数据存储。