Configuration

本节介绍 Spring Data LDAP 的配置。可以通过在你的 XML 配置中使用 <data-ldap:repositories> 标记或通过在配置类上使用 @EnableLdapRepositories 注解,启用 Spring LDAP 仓库:

要为自动生成的仓库包含对 LdapQuery 参数的支持,让你的界面扩展 LdapRepository 而非 CrudRepository。 所有 Spring LDAP 存储库都必须与使用 ODM 注解进行注释的实体一起使用,如 Object-Directory Mapping 中所述。 由于所有 ODM 托管类都必须拥有 Distinguished Name 作为 ID,因此所有 Spring LDAP 仓库都必须将 ID 类型参数设置为 javax.naming.Name。 事实上,内置的 LdapRepository 仅采用一个类型参数:托管实体类,它将 ID 默认设置为 javax.naming.Name。 由于 LDAP 协议的具体特性,无法支持 Spring LDAP 存储库的分页和排序。

您必须使用 ODM 注释,例如 org.springframework.ldap.odm.annotations.Id。使用 Spring Data 的注释不起作用,因为 Spring LDAP 使用自己的映射层。

Annotation-based Configuration

可以使用 JavaConfig 和自定义 XML 命名空间来激活 Spring Data LDAP 存储库支持,如下例所示:

Example 1. Spring Data LDAP repositories using JavaConfig
@Configuration
@EnableLdapRepositories("com.acme.*.repositories")
class MyConfig {

  @Bean
  ContextSource contextSource() {

    LdapContextSource ldapContextSource = new LdapContextSource();

    ldapContextSource.setUserDn("cn=Admin");
    ldapContextSource.setPassword("secret");
    ldapContextSource.setUrl("ldap://127.0.0.1:389");

    return ldapContextSource;
  }

  @Bean
  LdapTemplate ldapTemplate(ContextSource contextSource) {
    return new LdapTemplate(contextSource);
  }
}

此配置将扫描基础包,以查找扩展包含 LDAP 存储库并为此类存储库创建 Spring bean 的接口。

如果没有配置基础包,则基础架构将扫描带注释的配置类的包。

Spring Namespace

Spring Data 的 LDAP 模块包含一个允许定义存储库 bean 的自定义命名空间。它还包含一些特殊于 LDAP 的特性和元素特性。通常,可以使用 repositories 元素来设置 LDAP 存储库,如下例所示:

Example 2. Setting up LDAP repositories by using the namespace
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:ldap="http://www.springframework.org/schema/ldap"
  xmlns:data-ldap="http://www.springframework.org/schema/data/ldap"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/ldap
    https://www.springframework.org/schema/ldap/spring-ldap.xsd
    http://www.springframework.org/schema/data/ldap
    https://www.springframework.org/schema/data/ldap/spring-ldap.xsd">

  <ldap:context-source url="ldap://127.0.0.1:389"
                     username="cn=Admin"
                     password="secret" />

  <ldap:ldap-template />

  <data-ldap:repositories base-package="com.acme.*.repositories" />

</beans>

此配置将扫描基础包,以查找扩展包含 LDAP 存储库并为此类存储库创建 Spring bean 的接口。

默认情况下,存储库获取一个自动注入的 Spring bean LdapTemplate,名为 ldapTemplate,因此只有在偏离此约定时才需要显式配置 ldap-template-ref

哪种更好,JavaConfig 还是 XML?XML 是很久以前配置 Spring 的方法。在 Java、记录类型、注释等快速发展的当今时代,新的项目通常会尽可能多地使用纯 Java。虽然没有立即删除 XML 支持的计划,但某些最新功能可能无法通过 XML 获得。

使用 repositories 元素查找 Spring 数据存储库,如 Creating Repository Instances 中所述。

Custom Namespace Attributes

除了 default attributes of the repositories element,LDAP 名称空间还提供了其他属性,让你能够更详细地控制存储库的设置:

Table 1. Custom LDAP-specific attributes of the repositories element

ldap-template-ref

明确将 LdapTemplate 连接到 repositories 元素检测到的存储库中进行使用。当应用程序中使用多个 LdapTemplate Bean 时通常使用。如果未配置,Spring Data 会在 ApplicationContext 中自动查找名为 ldapTemplateLdapTemplate Bean。

Spring Data LDAP 需要存在一个名为 ldapMappingContextLdapMappingContext bean。如果没有定义此类 bean,则 Spring Data LDAP 会在应用程序上下文中注册一个默认实例。