真正使用Sring LDAP,肯定要根据情况进行AttributeMapper, 下面是我写的比较通用的实现方法,可以指定mapping哪个field 或是全部 /** * LDAPAttributeMapper * Feb.2009 * @author chao yin */ package com.springldap.demo; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; import javax.naming.NamingException; import javax.naming.directory.Attributes; import org.springframework.ldap.AttributesMapper; /** * * LDAP attribute mapper class * */ public class LDAPAttributeMapper implements AttributesMapper{ private String[] attrs; private Object dto;//attributeMapper Object public LDAPAttributeMapper(){} /** * * @param attrs custom attributes * @param dto attributes mapper object */ public LDAPAttributeMapper(String[] attrs,Object dto){ this.setAttrs(attrs); this.setDto(dto); } /** * implements AttributesMapper mapFromAttributes method mapping to a java object */ public Object mapFromAttributes(Attributes attributes) throws NamingException { try { Field []objectField=this.getDto().getClass().getDeclaredFields(); AccessibleObject.setAccessible(objectField,true); this.setDto(this.getDto().getClass().getConstructors()[0].newInstance(null));//create a new instance every record if(this.getAttrs()==null){// if doesn't set custom attributes then get all attributes for(int fieldIndex=0;fieldIndex < objectField.length; ++ fieldIndex){ String tmpAttrName=objectField[fieldIndex].getName(); if(attributes.get(tmpAttrName)!=null){//right attr name objectField[fieldIndex].set(this.getDto(),attributes.get(tmpAttrName).get()); } } }else{//attrs isn't null then get custom attributes value for(int attrsIndex=0;attrsIndex < this.getAttrs().length;++attrsIndex){ if(attributes.get(this.getAttrs()[attrsIndex])!=null){//right attr name String tmpAttrValue=(String)attributes.get(this.getAttrs()[attrsIndex]).get();//get relative attr value if(tmpAttrValue != null){ //set field value for(int fieldIndex=0;fieldIndex < objectField.length; ++ fieldIndex){ //set suitable field value if(this.getAttrs()[attrsIndex].toLowerCase().equals(objectField[fieldIndex].getName().toLowerCase())){ objectField[fieldIndex].set(this.getDto(), tmpAttrValue); } } } } } } }catch(Exception e){ e.printStackTrace(); } return dto; } public String[] getAttrs() { return attrs; } public void setAttrs(String[] attrs) { this.attrs = attrs; } public Object getDto() { return dto; } public void setDto(Object dto) { this.dto = dto; } }