Using DirContextSource with Spring LDAP

DirContextSource nicely intergrates with Spring LDAP by providing a thin wrapper around DirContextSource.Builder exposed as a ContextSource implementation: net.sf.michaelo.dirctxsrc.spring.DirContextSource.

Contrary to previous statements, this library has to reside in the webapp classpath along with its dependencies (spring-ldap-core 2.2.0.RELEASE+ and spring-beans 4.3.4.RELEASE+) to work properly.

In the first step, you have to tell Spring to create a bean and in the second step, let Spring autowire this bean to your class.

Add this to your beans.xml, e.g., root-context.xml or servlet-context.xml:

<beans …>
  <!-- Add this -->
  <beans:bean class="net.sf.michaelo.dirctxsrc.spring.DirContextSource">

Now wire this to your class of choice:

  private ContextSource contextSource;
  // Alternatively, you can use setter injection
  public void myMethod() {
    // Now use the context source as same as in a regular webapp along with the LdapTemplate