原题目:Spring Data LDAP参考文档(内容起源:Spring中国教育管理中心)
本章指出了 LDAP 存储库反对的个性。它建设在应用 Spring Data Repositories 中解释的外围存储库反对上。您应该对那里解释的基本概念有充沛的理解。
在应用 Spring LDAP 存储库时,您应该记住以下几点:
Spring LDAP 存储库能够通过<data-ldap:repositories>在 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 }
Spring认证中国教育管理中心-Spring认证干货教程
咱们这里有一个简略的域对象。请留神,它有一个名为dntype的属性Name。应用该域对象,咱们能够通过为它定义一个接口来创立一个存储库来长久化该类型的对象,如下所示:
示例 55. 用于长久化Person实体的根本存储库接口
public interface PersonRepository extends CrudRepository<Person, Long> {
// additional custom finder methods go here
}
目前,此接口仅用于键入目标,但咱们能够稍后为其增加其余办法。在您的 Spring 配置中,增加以下内容:
示例 56.惯例 LDAP 存储库 Spring 配置
<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>
Spring认证中国教育管理中心-Spring认证干货教程
此命名空间元素会导致扫描根底包以查找LdapRepository为找到的每个扩大和创立 Spring bean 的接口。默认状况下,存储库会取得一个主动拆卸的LdapTemplateSpring bean,该 bean 称为ldapTemplate,因而ldap-template-ref如果您偏离此约定,则只须要显式配置。
如果要应用 Java 配置,请应用@EnableLdapRepositories正文。正文带有与命名空间元素雷同的属性。如果没有配置根本包,基础设施会扫描带正文的配置类的包。以下示例显示了如何设置 Java 配置:
示例 57. 存储库的 Java 配置
@Configuration
@EnableLdapRepositories
class ApplicationConfig {
@BeanContextSource contextSource() { LdapContextSource ldapContextSource = new LdapContextSource(); ldapContextSource.setUrl("ldap://127.0.0.1:389"); return ldapContextSource;}@BeanLdapTemplate ldapTemplate(ContextSource contextSource) { return new LdapTemplate(contextSource);}
}
Spring认证中国教育管理中心-Spring认证干货教程
因为咱们的域存储库扩大了CrudRepository,它为您提供了 CRUD 操作以及拜访实体的办法。应用存储库实例是将其注入客户端的依赖关系。
咱们能够为咱们的存储库增加分页拜访,如下所示:
示例 58. 对 Person 实体的分页拜访
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class PersonRepositoryTests {
@Autowired PersonRepository repository;@Testpublic void readAll() { List<Person> persons = repository.findAll(); assertThat(persons.isEmpty(), is(false));}
}
Spring认证中国教育管理中心-Spring认证干货教程
该示例应用 Spring 的单元测试反对创立了一个应用程序上下文,它将执行基于注解的依赖注入到测试用例中。在测试方法中,咱们应用存储库来查问数据存储。
7.2.查询方法
您通常在存储库上触发的大多数数据拜访操作都会导致对 LDAP 目录运行查问。定义这样的查问就是在存储库接口上申明一个办法,如以下示例所示:
示例 59.带有查询方法的 PersonRepository
public interface PersonRepository extends PagingAndSortingRepository<Person, String> {
List<Person> findByLastname(String lastname); List<Person> findByLastnameFirstname(String lastname, String firstname);
}
该办法显示对所有具备给定 的人的查问lastname。该查问是通过解析能够与And和连贯的束缚的办法名称来派生的Or。因而,办法名称导致查问表达式为(&(objectclass=person)(lastname=lastname))。
该办法显示了对所有具备给定lastname和 的人的查问firstname。查问是通过解析办法名称得出的。因而,办法名称导致查问表达式为(&(objectclass=person)(lastname=lastname)(firstname=firstname))。
Spring认证中国教育管理中心-Spring认证干货教程
下表提供了可用于查询方法的关键字示例:
Spring认证中国教育管理中心-Spring认证干货教程
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@ApplicationScopedpublic LdapOperations createLdapTemplate() { ContextSource contextSource = … return new LdapTemplate(contextSource);}
}
Spring认证中国教育管理中心-Spring认证干货教程
LdapTemplate每当容器申请存储库类型的 bean 时,Spring Data LDAP CDI 扩大都会将其作为 CDI bean 并为 Spring Data 存储库创立代理。因而,获取 Spring Data 存储库的实例是申明注入属性的问题,如以下示例所示:
class RepositoryClient {
@Inject
PersonRepository repository;
public void businessMethod() {
List<Person> people = repository.findAll();
}
}
内容起源:Spring中国教育管理中心(Spring认证)
Spring认证中国教育管理中心-Spring认证干货教程
2021年2月,VMware公司正式与北京中科卓望网络科技有限公司(以下简称:中科卓望)达成策略单干,授予其 Spring 中国教育管理中心,携手 VMware 寰球最新 Spring技术和认证体系,帮忙中国院校构建业余教学内容,全面赋能将来开发人。