真正使用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; } }
背景 spring和mybatis集成過程中,我們可以通過MapperFactoryBean的方式配置Mapper接口。但是這樣需要在配置文件中,爲每個mapper配置相同的代碼塊,浪費時間。關鍵對於代碼潔癖的人來說,一點不能忍。 <bea
爲什麼要研究字符串? 人機交互的過程中,文字、數字、字母、符號都是字符表現形式,這部分內容佔了人機信息交互的大部分內容,所以有必要明確一些基本問題。因此大部分數據類型都應該有字符串表達形式,我們在定義新類型的時候可以根據需要來定義新類型的
security的簡單原理: 使用衆多的攔截器對url攔截,以此來管理權限。但是這麼多攔截器,不可能對其一一來講,主要講裏面核心流程的兩個。 首先,權限管理離不開登陸驗證的,所以登陸驗證攔截器AuthenticationProcessing
我們經常會需要啓動多個實例的情況來測試註冊中心、配置中心等基礎設施的高可用,也會用來測試客戶端負載均衡的調用等。但是,我們一個應用只能有一個端口號,這就使得在本機測試的時候,不得不爲同一個服務設置不同的端口來進行啓動。 在本地用不同端口啓動
簡介 Java Reflection,稱爲 Java 反射,是Java基礎部分的一個比較難的點。Reflection(反射)是被視爲動態語言的關鍵,通過反射機制,我們可以在運行時(runtime)獲取類的完整結構。例如,可以獲取到
一面 1. 簡短自我介紹 2. 事務的ACID,其中把事務的隔離性詳細解釋一遍 3. 髒讀、幻影讀、不可重複讀 4. 紅黑樹、二叉樹的算法 5. 平常用到哪些集合類?ArrayList和LinkedList區別?HashMap內部數據結構
當我們站在技術之路的原點,未來可能充滿了迷茫,也存在着很多不同的可能。在這個知識爆炸與終身學習/碎片化學習爲主的時代,我們面臨的問題之一就是如何進行有效學習,不僅能有效平衡廣度與深度,並且能真正的積澱下來,提升自己的研發效能。於筆者而
阿里巴巴Java開發手冊 梳理筆記 - finally 塊必須對資源對象、流對象進行關閉 規約內容: 2.1 異常處理 6. 【強制】 finally 塊必須對資源對象、流對象進行關閉,有異常也要做 try - catch 。 說明:如果
在Java方面,能夠實現多線程安全修改對象值得方法只有2個 1.原子操作 2.互斥方法 而在Java當中,或是其他語言中,基本上也都是使用CAS實現。CAS是比較並交換的意思,這個操作包含2個連續的操作,比較,還有賦值,因爲2個操作在cpu
單線程輪詢並執行任務,有可能會導致飢餓。比較適合於一些具有一個任務的週期調度。 任務列表用數組存儲,元素最前面的元素是最先被執行的任務。 schedule方法是固定間隔執行,根據上一次執行的結束時間來定義間隔的開始時間 scheduleAt
創建的ReadWriteLock需要使用兩個方法來繼續創造2個對象,分別是ReadLock和WriteLock。 這兩個鎖對象的方法調用都會匯聚到ReadWriteLock的Sync類中。多個對象的方法調用匯聚到一個對象上面,這個設計模式是
synchronized關鍵字可以修飾普通方法,靜態方法,當修飾普通方法,鎖對象是當前對象,當修飾靜態方法,鎖對象是當前類的class synchroinzed關鍵字可以使用同步代碼塊實現同步,此時可以指定鎖對象。 同步方法的實現方式 同步
AQS是Java多線程編程的重入鎖,管程,工具類的基礎類,是必須要掌握的。不掌握這個類,根本不能稱之爲合格的Java程序員。 即使是把這個類所有的代碼都背會,也是值得的。 如何標識已經有線程在執行呢? 有兩個變量,一個state變量,一個e
################這是之前的思考 內存屏障只是保證清空流水線,如何保證高速緩存的內容更新到最新或刷新到主存呢?這個問題突然想到了,不知道這個需要怎回答。 內存屏障保證的CPU執行執行序列能夠順序執行,而不是亂序執行。當然,前提
使用volidate關鍵字修飾的成員變量,保證可見性,也就是當一個線程修改這樣的成員變量後,其他的線程能夠立即看到這次的修改。有兩個方面的原因: 1.編譯器不優化對這樣的關鍵字修飾變量的一些訪問操作 2.對讀,寫這個關鍵字修飾的變量前後,會