Spring LDAP - Creation of LdapTemplate in standalone java program - Using Spring LDAP as CDI Resource

VirtualLogic picture VirtualLogic · Mar 16, 2014 · Viewed 8.3k times · Source

I am trying to construct a LdapTemplate object of using spring data.

 public class LDAPTemplate {

        public static void main(String[] args) {
            LdapContextSource lcs = new LdapContextSource();
            lcs.setUrl("ldap://localhost:389/");
            lcs.setUserDn("cn=Manager, dc=example, dc=com");
            lcs.setPassword("secret1");
            lcs.setDirObjectFactory(DefaultDirObjectFactory.class);
            LdapTemplate ldap = new LdapTemplate(lcs);
            ldap.lookup("cn=aaa");

        }

    }

I wanted to know is that the right way to instantiate ldap template object. Because when I perform a lookup, it throws NPE.

I am trying to use LDAP Spring in CDI context without using spring at all. If you have pointers on that would be nice. Does Spring LDAP is dependent on spring?

Answer

Pavel Horal picture Pavel Horal · Mar 16, 2014

LdapContextSource is InitializingBean so you need to call afterPropertiesSet...

And the JavaDoc:

When using implementations of this class outside of a Spring Context it is necessary to call afterPropertiesSet() when all properties are set, in order to finish up initialization.