1:前言
本例只在spring-ldap-1.2-RC1.jar下测试通过,sping下的LDAP配置和操作,都类似于JdbcTemplate,所以只要熟悉JdbcTemplate,那么LDAP的配置就显得非常简单。LDAP本身不提供事务,但是Spring使用删除操作来模仿事务。
2:配置LDAP数据源和LdapTemplate
<bean id="ldapSource"
class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="ldap://192.168.16.79:10000" />
<property name="base" value="dc=somc,dc=com" />
<property name="userDn" value="cn=admin" />
<property name="password" value="19820914" />
</bean>
<bean id="ldapTemplate"
class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="ldapSource" />
</bean>
<bean id="userDaoLdapImpl" class="UserDaoLdapImpl" />
3:查询代码
import javax.naming.directory.Attributes;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.transaction.compensating.manager.ContextSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
/**
*
*
* @author kiral
* @date 2008-12-16
*/
public class UserDaoLdapImpl {
private LdapTemplate ldapTemplate;
private ContextSourceTransactionManager transactionManager;
public void setTransactionManager(ContextSourceTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
public Person getPersonByDn(String dn) {
new TransactionTemplate(transactionManager)
.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// TODO Auto-generated method stub
}
});
return (Person) ldapTemplate.lookup(dn, new PersonAttributesMapper());
}
private class PersonAttributesMapper implements AttributesMapper {
public Object mapFromAttributes(Attributes attrs)
throws javax.naming.NamingException {
Person person = new Person();
person.setUserCode((String) attrs.get("userCode").get());
return person;
}
}
}
4:测试代码
import com.ultrapower.somc.managerdashboard.test.DaoTestCase;
public class UserDaoLdapImplTest extends DaoTestCase {
private UserDaoLdapImpl userDaoLdapImpl;
public void setUserDaoLdapImpl(UserDaoLdapImpl userDaoLdapImpl) {
this.userDaoLdapImpl = userDaoLdapImpl;
}
public void testAll() {
String dn = "userCode=user001,ou=somcUser";
String userCode = "user001";
Person person = userDaoLdapImpl.getPersonByDn(dn);
assertTrue(userCode.equals(person.getUserCode()));
}
}
分享到:
相关推荐
electron-quick-start-master 快速入门教程electron-quick-start-master 快速入门教程electron-quick-start-master 快速入门教程electron-quick-start-master 快速入门教程electron-quick-start-master 快速入门教程...
You will begin with covering the basics of Truffle, briefly explaining how it integrates Solidity and Web3, in orderto start building a mini decentralized application. Also, you will dive into ...
"Quick Start V1.0" 是一款旨在帮助用户简化桌面管理、提高启动效率的软件工具。这款工具的主要功能是让用户能够快速地访问和启动常用的应用程序,从而避免桌面图标过多导致的混乱,同时也能提高用户的操作速度。 ...
本教程《Spring Quick Start Tutorial.pdf》中可能涵盖以下内容: 1. **Spring概述**:介绍Spring框架的发展历程、设计理念和主要功能,解释为什么Spring成为企业级Java开发的首选框架。 2. **环境搭建**:指导...
Word2013快速入门指南、 Word 2013 Quick Start Guide、英文版
Build web applications with MongoDB, ExpressJS, React, and Node
Blender Quick Start Guide
《Activiti Developer Quick Start快速入门指南》 Activiti是一款开源的工作流引擎,它基于Java,设计用于企业级应用,提供了一套强大的业务流程自动化(BPM)解决方案。本指南将帮助开发者快速上手Activiti,从...
TensorFlow Reinforcement Learning Quick Start Guide: Get up and running with training and deploying intelligent, self-learning agents using Python Author: Kaushik Balakrishnan Pub Date: 2019 ISBN: 978...
### CC2530快速入门指南 #### 一、CC2530简介 CC2530是一款高性能、低成本的无线微控制器,专为2.4GHz IEEE 802.15.4、ZigBee和RF4CE应用而设计。它集成了强大的8051 MCU、用于物理层与MAC层的RF收发器、内存以及...
cocos构建发布,选择发布平台Web Mobile,构建成功将根目录build下生成的web-mobile整个文件夹拷贝到electron-quick-start-exe根目录中,然后执行npm start可以预览效果,npm run package可以进行打包(第一次可能有...
在这个“TensorFlow 2.0 Quick Start”指南中,我们将深入探讨这个新版本的关键特性,以及如何快速上手。 **一、核心概念与安装** 1. **核心API**: TensorFlow 2.0 强调了Eager Execution(即时执行),这是一种...
Quick Start Guides are focused, shorter titles that provide a faster paced introduction to a technology. They are for people who don't need all the detail at this point in their learning curve. The ...
Google Flutter Mobile Development Quick Start Guide - Salvatore Giordano & Prajyot Mainkar(2019),epub格式,欢迎下载