Spring LDAP-在独立的Java程序中创建LdapTemplate-使用Spring LDAP作为CDI资源

Vir*_*gic 5 spring ldap spring-ldap

我正在尝试使用弹簧数据构造一个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");

        }

    }
Run Code Online (Sandbox Code Playgroud)

我想知道的是实例化ldap模板对象的正确方法。因为当我执行查找时,它会抛出NPE。

我正在尝试在CDI上下文中使用LDAP Spring,而根本不使用spring。如果您有指针,那就太好了。Spring LDAP是否依赖于spring?

Pav*_*ral 5

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

And the JavaDoc:

在 Spring Context 之外使用此类的实现时,必须在设置所有属性后调用 afterPropertiesSet() 以完成初始化。