跳到主要内容

配置

QWen Flash 中英对照 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 使用自己的映射层。

基于注解的配置​

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

示例 1. 使用 JavaConfig 的 Spring Data LDAP 存储库

@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);
}
}
java

此配置会导致扫描基本包以查找扩展包含 LDAP 存储库的接口,并为每个找到的接口创建 Spring bean。

未配置 base package 时,基础设施会扫描带注解的 configuration class 所在的包。

Spring Namespace​

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

示例 2. 使用 namespace 设置 LDAP 仓库

<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>
xml

此配置会导致扫描基础包,以查找扩展或包含 LDAP 仓库的接口,并为每个找到的接口创建 Spring bean。

默认情况下,repositories 会获得一个自动装配的 LdapTemplate Spring bean,其名称为 ldapTemplate,因此只有当你偏离此约定时,才需要显式配置 ldap-template-ref。

提示

JavaConfig 和 XML 哪个更好?XML 是 Spring 很早以前的配置方式。在当今 Java 快速发展的时代,有 record types、annotations 等,新项目通常会尽可能使用纯 Java。虽然目前没有立即移除 XML 支持的计划,但一些最新特性 MAY 无法通过 XML 使用。

使用 repositories 元素会查找 Spring Data 仓库,如 创建仓库实例 中所述。

自定义命名空间属性​

除了 repositories 元素的默认属性 之外,LDAP 命名空间还提供了额外的属性,以便你对 repositories 的设置进行更详细的控制:

表 1. repositories 元素的自定义 LDAP 特定属性

ldap-template-ref显式地配置与由 repositories 元素检测到的仓储一起使用的 LdapTemplate。通常在应用中使用多个 LdapTemplate bean 时使用。如果未配置,Spring Data 会自动在 ApplicationContext 中查找名为 ldapTemplate 的 LdapTemplate bean。
备注

Spring Data LDAP 需要一个名为 ldapMappingContext 的 LdapMappingContext bean 存在。如果没有定义这样的 bean,则 Spring Data LDAP 会在 application context 中注册一个默认实例。