Configuration

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

This section describes configuring Spring Data LDAP. Spring LDAP repositories can be enabled by using a <data-ldap:repositories> tag in your XML configuration or by using an @EnableLdapRepositories annotation on a configuration class:

要为自动生成的仓库包含对 LdapQuery 参数的支持,让你的界面扩展 LdapRepository 而非 CrudRepository

To include support for LdapQuery parameters in automatically generated repositories, have your interface extend LdapRepository rather than CrudRepository.

所有 Spring LDAP 存储库都必须与使用 ODM 注解进行注释的实体一起使用,如 Object-Directory Mapping 中所述。

All Spring LDAP repositories must work with entities annotated with the ODM annotations, as described in Object-Directory Mapping.

由于所有 ODM 托管类都必须拥有 Distinguished Name 作为 ID,因此所有 Spring LDAP 仓库都必须将 ID 类型参数设置为 javax.naming.Name

Since all ODM managed classes must have a Distinguished Name as the ID, all Spring LDAP repositories must have the ID type parameter set to javax.naming.Name.

事实上,内置的 LdapRepository 仅采用一个类型参数:托管实体类,它将 ID 默认设置为 javax.naming.Name

Indeed, the built-in LdapRepository only takes one type parameter: the managed entity class, which defaults the ID to javax.naming.Name.

由于 LDAP 协议的具体特性,无法支持 Spring LDAP 存储库的分页和排序。

Due to specifics of the LDAP protocol, paging and sorting are not supported for Spring LDAP repositories.

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

You must use ODM annotations, such as org.springframework.ldap.odm.annotations.Id. Using Spring Data’s annotation does not work, because Spring LDAP uses its own mapping layer.

Annotation-based Configuration

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

The Spring Data LDAP repositories support can be activated through both JavaConfig as well as a custom XML namespace, as shown in the following example:

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 的接口。

This configuration causes the base packages to be scanned for interfaces that extend contain LDAP repositories and create Spring beans for each one found.

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

If no base package is configured, the infrastructure scans the package of the annotated configuration class.

Spring Namespace

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

The LDAP module of Spring Data contains a custom namespace that allows defining repository beans. It also contains certain features and element attributes that are special to LDAP. Generally, the LDAP repositories can be set up by using the repositories element, as shown in the following example:

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 的接口。

This configuration causes the base packages to be scanned for interfaces that extend contain LDAP repositories and create Spring beans for each one found.

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

By default, the repositories get an autowired LdapTemplate Spring bean that is called ldapTemplate, so you only need to configure ldap-template-ref explicitly if you deviate from this convention.

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

Which is better, JavaConfig or XML? XML is how Spring was configured long ago. In today’s era of fast-growing Java, record types, annotations, and more, new projects typically use as much pure Java as possible. While there is no immediate plan to remove XML support, some of the newest features MAY not be available through XML.

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

Using the repositories element looks up Spring Data repositories as described in Creating Repository Instances.

Custom Namespace Attributes

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

Beyond the default attributes of the repositories element, the LDAP namespace offers additional attributes to let you gain more detailed control over the setup of the repositories:

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

ldap-template-ref

Explicitly wire the LdapTemplate to be used with the repositories being detected by the repositories element. Usually used if multiple LdapTemplate beans are used within the application. If not configured, Spring Data automatically looks up the LdapTemplate bean with the name ldapTemplate in the ApplicationContext.

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

Spring Data LDAP requires a LdapMappingContext bean named ldapMappingContext to be present. If no such bean is defined, then Spring Data LDAP registers a default instance in the application context.