我正在尝试使用 spring 数据构造一个 LdapTemplate 对象。
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");
}
}
我想知道实例化 ldap 模板对象的正确方法。因为当我执行查找时,它会抛出 NPE。
我正在尝试在 CDI 上下文中使用 LDAP Spring,而不使用 spring。如果你能指点一下那就太好了。 Spring LDAP 是否依赖于 spring?
最佳答案
LdapContextSource
是 InitializingBean
所以你需要调用 afterPropertiesSet
...
和 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.
关于Spring LDAP - 在独立 java 程序中创建 LdapTemplate - 使用 Spring LDAP 作为 CDI 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22435932/