要访问存储在符合 LDAP 的目录中的域实体,您可以使用我们复杂的存储库支持,这大大简化了实施。 为此,请为存储库创建一个接口,如以下示例所示:
例 1.Sample 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
}
由于我们的域存储库扩展了 ,它为您提供了 CRUD 操作以及访问实体的方法。
使用存储库实例是一个依赖关系问题,将其注入客户端。CrudRepository
例 3.访问个人实体
@ExtendWith(SpringExtension.class)
@ContextConfiguration
class PersonRepositoryTests {
@Autowired PersonRepository repository;
@Test
void readAll() {
List<Person> persons = repository.findAll();
assertThat(persons.isEmpty(), is(false));
}
}
该示例使用 Spring 的单元测试支持创建一个应用程序上下文,该上下文将执行基于注释的依赖项注入到测试用例中。 在测试方法中,我们使用存储库来查询数据存储。