Usage

为了访问存储在 LDAP 兼容目录中的域实体,你可以使用我们的复杂仓库支持,这将极大地简化实施。为此,创建一个仓库界面,如下例所示:

To access domain entities stored in a LDAP-compliant directory, you can use our sophisticated repository support that significantly eases implementation. To do so, create an interface for your repository, as the following example shows:

Example 1. Sample Person entity
@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。利用该域对象,我们可以通过定义一个界面,为该类型的对象创建仓库以使其持久化,如下所示:

We have a simple domain object here. Note that it has a property named dn of type Name. With that domain object, we can create a repository to persist objects of that type by defining an interface for it, as follows:

Example 2. Basic repository interface to persist Person entities
public interface PersonRepository extends CrudRepository<Person, Long> {

  // additional custom finder methods go here
}

由于我们的域仓库扩展了 CrudRepository,因此它为你提供了 CRUD 操作以及访问实体的方法。与仓库实例合作是将其依赖项注入客户端的问题。

Because our domain repository extends CrudRepository, it provides you with CRUD operations as well as methods for access to the entities. Working with the repository instance is a matter of dependency injecting it into a client.

Example 3. Access to Person entities
@ExtendWith(SpringExtension.class)
@ContextConfiguration
class PersonRepositoryTests {

    @Autowired PersonRepository repository;

    @Test
    void readAll() {

      List<Person> persons = repository.findAll();
      assertThat(persons.isEmpty(), is(false));
    }
}

示例使用 Spring 的单元测试支持创建了一个应用程序上下文,它将在测试用例中执行基于注释的依赖项注入。在测试方法内,我们使用仓库查询数据存储。

The sample creates an application context with Spring’s unit test support, which will perform annotation-based dependency injection into test cases. Inside the test method, we use the repository to query the datastore.