2012-10-05 41 views
0

我一直在努力搜索LDAP目錄中的所有用戶。當我執行搜索時,它將作爲目錄中正確數量的條目返回,但它是同一條目(最後一條)的所有副本。我似乎無法弄清楚這是爲什麼。下面是我的一些代碼片段:Ldaptemplate搜索返回相同條目的正確次數

功能實際執行搜索:

public List<User> getAllUsers(){ 
    AndFilter filter = new AndFilter(); 
    filter.and(new EqualsFilter("objectclass", "person")); 
    List<User> users = ldapTemplate.search(DistinguishedName.EMPTY_PATH, 
          filter.encode(), new UserAttributesMapper()); 

    return users; 
} 

用戶僅僅是一個getter和設置類,它包含所有關於用戶的信息。該UserAttributesMapper如下:

private static class UserAttributesMapper implements AttributesMapper { 
    public Object mapFromAttributes(Attributes attrs) throws NamingException { 
     ... 
     User user = (User) AppUtils.getBean("user");  
     NamingEnumeration ae = attrs.getAll(); 
     ... 
     //set user attributes through setters 
     // ie: if(attrs.get("uid") != null) user.setUid((String) attrs.get("uid").get()); 
     ... 
     return user; 
    } 
} 

我知道映射的作品,因爲我可以,沒有問題返回一個用戶,它精美的作品。我只是不明白爲什麼它只返回List只有最後一個用戶條目。我的一個想法是在聲明中

User user = (User) AppUtils.getBean("user"); 

User類都被註解@Component("user")getBean功能是

public static Object getBean(final String name) { 
    if(applicationContext == null) { 
     throw new IllegalArgumentException(
       "ApplicationContext is not initialized"); 
    } 
    return applicationContext.getBean(name); 

任何幫助將不勝感激使用static

回答

0

很明顯,您在列表的每個位置都使用相同的用戶對象,因此您不斷更新同一用戶對象,而不是爲每個搜索結果創建一個新對象。

+0

我不知道我是如何忽略這麼簡單的東西,但謝謝! – James

相關問題