title: Spring LDAP, setting connection details in java
body: <p>I want to set LDAP connection to list all users from AD.
  I successfully accomplished this with information stored in XML</p>
  
  <pre><code>&lt;ldap:context-source
  url="ldap://&lt;url&gt;"
  base="dc=example,dc=local"
  username="&lt;user&gt;@example.local"
  password="&lt;pass&gt;" /&gt;
  </code></pre>
  
  <p>But how I can set this informations from Java, not in XML?
  Tried with:</p>
  
  <pre><code>LdapContextSource ctxSrc = new LdapContextSource();
      ctxSrc.setUrl("ldap://&lt;url&gt;");
      ctxSrc.setBase("dc=example,dc=local");
      ctxSrc.setUserDn("&lt;user&gt;@example.local");
      ctxSrc.setPassword("&lt;pass&gt;");
  LdapTemplate tmpl = new LdapTemplate(ctxSrc);
  setLdapTemplate(tmpl);
  </code></pre>
  
  <p>But when runing</p>
  
  <p><code>List users = (List&lt;User&gt;) ldapTemplate.search(LdapUtils.emptyLdapName(), "(&amp;(objectCategory=person)(objectClass=user))", new UserAttributesMapper());</code></p>
  
  <p>I get <strong>NullPointerExeption</strong>. Runing that without setting up properties from java (i.e. reading from xml) everything works fine</p>