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?
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.
Correct Code
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);
lcs.afterPropertiesSet();
LdapTemplate ldap = new LdapTemplate(lcs);
ldap.lookup("cn=aaa");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With