Spring认证中国教育管理中心-Spring认
寂寞的心情wang 发布于2021-11 浏览:3016 回复:0
0
收藏

原标题:Spring Data LDAP参考文档(内容来源:Spring中国教育管理中心)

本章指出了 LDAP 存储库支持的特性。它建立在使用 Spring Data Repositories 中解释的核心存储库支持上。您应该对那里解释的基本概念有充分的了解。

在使用 Spring LDAP 存储库时,您应该记住以下几点:

Spring LDAP 存储库可以通过在 XML 配置中使用标记或@EnableLdapRepositories在配置类上使用注释来启用。
要LdapQuery在自动生成的存储库中包含对参数的支持,请让您的界面扩展LdapRepository而不是CrudRepository.
所有 Spring LDAP 存储库都必须与使用 ODM 注释注释的实体一起使用,如Object-Directory Mapping 中所述。
由于所有 ODM 托管类都必须有一个专有名称作为 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 使用了自己的映射层。

7.1.用法
要访问存储在 LDAP 兼容目录中的域实体,您可以使用我们复杂的存储库支持,这大大简化了实施。为此,请为您的存储库创建一个接口,如以下示例所示:

示例 54. 示例 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 }

 
我们这里有一个简单的域对象。请注意,它有一个名为dntype的属性Name。使用该域对象,我们可以通过为它定义一个接口来创建一个存储库来持久化该类型的对象,如下所示:

示例 55. 用于持久化Person实体的基本存储库接口

public interface PersonRepository extends CrudRepository {

// additional custom finder methods go here
}
目前,此接口仅用于键入目的,但我们可以稍后为其添加其他方法。在您的 Spring 配置中,添加以下内容:

示例 56.常规 LDAP 存储库 Spring 配置

    

 
此命名空间元素会导致扫描基础包以查找LdapRepository为找到的每个扩展和创建 Spring bean 的接口。默认情况下,存储库会获得一个自动装配的LdapTemplateSpring bean,该 bean 称为ldapTemplate,因此ldap-template-ref如果您偏离此约定,则只需要显式配置。

如果要使用 Java 配置,请使用@EnableLdapRepositories注释。注释带有与命名空间元素相同的属性。如果没有配置基本包,基础设施会扫描带注释的配置类的包。以下示例显示了如何设置 Java 配置:

示例 57. 存储库的 Java 配置

@Configuration
@EnableLdapRepositories
class ApplicationConfig {

@Bean
ContextSource contextSource() {

LdapContextSource ldapContextSource = new LdapContextSource();
ldapContextSource.setUrl("ldap://127.0.0.1:389");

return ldapContextSource;
}

@Bean
LdapTemplate ldapTemplate(ContextSource contextSource) {
return new LdapTemplate(contextSource);
}
}
 
因为我们的域存储库扩展了CrudRepository,它为您提供了 CRUD 操作以及访问实体的方法。使用存储库实例是将其注入客户端的依赖关系。

我们可以为我们的存储库添加分页访问,如下所示:

示例 58. 对 Person 实体的分页访问

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class PersonRepositoryTests {

@Autowired PersonRepository repository;

@Test
public void readAll() {

List persons = repository.findAll();
assertThat(persons.isEmpty(), is(false));
}
}
 
该示例使用 Spring 的单元测试支持创建了一个应用程序上下文,它将执行基于注解的依赖注入到测试用例中。在测试方法中,我们使用存储库来查询数据存储。

7.2.查询方法
您通常在存储库上触发的大多数数据访问操作都会导致对 LDAP 目录运行查询。定义这样的查询就是在存储库接口上声明一个方法,如以下示例所示:

示例 59.带有查询方法的 PersonRepository

public interface PersonRepository extends PagingAndSortingRepository {

List findByLastname(String lastname);

List findByLastnameFirstname(String lastname, String firstname);
}
该方法显示对所有具有给定 的人的查询lastname。该查询是通过解析可以与And和连接的约束的方法名称来派生的Or。因此,方法名称导致查询表达式为(&(objectclass=person)(lastname=lastname))。

该方法显示了对所有具有给定lastname和 的人的查询firstname。查询是通过解析方法名称得出的。因此,方法名称导致查询表达式为(&(objectclass=person)(lastname=lastname)(firstname=firstname))。

 
下表提供了可用于查询方法的关键字示例:

 
7.2.1.查询DSL支持
Spring LDAP 中包含基本的 QueryDSL 支持。这种支持包括以下内容:

Annotation Processor,LdapAnnotationProcessor用于基于 Spring LDAP ODM 注释生成 QueryDSL 类。有关ODM 注释的更多信息,请参阅对象目录映射。
查询实现,QueryDslLdapQuery用于在代码中构建和运行 QueryDSL 查询。
Spring Data 存储库支持 QueryDSL 谓词。QueryDslPredicateExecutor包括许多具有适当参数的附加方法。您可以扩展此接口(连同LdapRepository)以将此支持包含在您的存储库中。
7.3.各种各样的
7.3.1.CDI集成
存储库接口的实例通常由容器创建,因此在使用 Spring Data 时,Spring 是最自然的选择。从 version 2.1 开始,Spring Data LDAP 包含一个自定义 CDI 扩展,允许您在 CDI 环境中使用存储库抽象。该扩展是 JAR 的一部分。要激活它,请将 Spring Data LDAP JAR 放入您的类路径中。您现在可以通过为 实现 CDI Producer 来设置基础结构LdapTemplate,如以下示例所示:

class LdapTemplateProducer {

@Produces
@ApplicationScoped
public LdapOperations createLdapTemplate() {

ContextSource contextSource = …
return new LdapTemplate(contextSource);
}
}
 
LdapTemplate每当容器请求存储库类型的 bean 时,Spring Data LDAP CDI 扩展都会将其作为 CDI bean 并为 Spring Data 存储库创建代理。因此,获取 Spring Data 存储库的实例是声明注入属性的问题,如以下示例所示:

class RepositoryClient {

@Inject
PersonRepository repository;

public void businessMethod() {
List people = repository.findAll();
}
}
内容来源:Spring中国教育管理中心(Spring认证)

 
2021年2月,VMware公司正式与北京中科卓望网络科技有限公司(以下简称:中科卓望)达成战略合作,授予其 Spring 中国教育管理中心,携手 VMware 全球最新 Spring技术和认证体系,帮助中国院校构建专业教学内容,全面赋能未来开发人。

收藏
点赞
0
个赞
TOP
切换版块