用法
要访问存储在 LDAP 兼容目录中的领域实体,可以使用我们成熟的 repository 支持,它显著简化了实现。为此,请为你的 repository 创建接口,如下例所示:
示例 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 实体的基本 repository 接口
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 的单元测试支持创建应用上下文,该支持会对测试用例执行基于注解的依赖注入。在测试方法内部,我们使用 repository 查询 datastore。