Spring LDAP PoolingContextSource via annotation

2019-07-24 03:51发布

问题:

I was trying to convert the Spring LDAP PoolingContextSource XML configuration to use annotation. I am able to get the LdapContextSource working by following the one mention here, but I can't get the PoolingContextSource working. When I ran the code, I got NullPointerException. XML, annotation and exception snippet are listed below.

XML configuration snippet,

<ldap:context-source
        id="ldapContextSource"
        username="${ldap.username}"
        password="${ldap.password}"
        url="${ldap.url}"
        base="${ldap.base}">
    <ldap:pooling
            test-on-borrow="true"
            test-while-idle="true"/>
</ldap:context-source>

<ldap:ldap-template id="ldapTemplate" context-source-ref="ldapContextSource"/>

Annotation configuration snippet,

@Bean
public ContextSource ldapContextSource() {
    LdapContextSource contextSource = new LdapContextSource();
    contextSource.setUrl(ldapUrl);
    contextSource.setBase(ldapBase);
    contextSource.setUserDn(ldapUsername);
    contextSource.setPassword(ldapPassword);

    PoolingContextSource poolingContextSource = new PoolingContextSource();
    poolingContextSource.setDirContextValidator(new DefaultDirContextValidator());
    poolingContextSource.setContextSource(contextSource);
    poolingContextSource.setTestOnBorrow(true);
    poolingContextSource.setTestWhileIdle(true);

    TransactionAwareContextSourceProxy proxy = new TransactionAwareContextSourceProxy(poolingContextSource);

    return proxy;
}

@Bean
public LdapTemplate ldapTemplate() {
    return new LdapTemplate(ldapContextSource());
}

Exception I got,

Exception in thread "main" org.springframework.dao.DataAccessResourceFailureException: Failed to borrow DirContext from pool.; nested exception is java.lang.NullPointerException
at org.springframework.ldap.pool.factory.PoolingContextSource.getContext(PoolingContextSource.java:446)
at org.springframework.ldap.pool.factory.PoolingContextSource.getReadWriteContext(PoolingContextSource.java:429)
at org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy.getReadWriteContext(TransactionAwareContextSourceProxy.java:88)
at org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy.getReadOnlyContext(TransactionAwareContextSourceProxy.java:61)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:357)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:309)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:642)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:578)
at org.springframework.ldap.core.LdapTemplate.find(LdapTemplate.java:1836)
at org.springframework.ldap.core.LdapTemplate.find(LdapTemplate.java:1857)
at org.springframework.ldap.core.LdapTemplate.findOne(LdapTemplate.java:1865)
at org.example.playground.ldap.spring.PersonDaoImpl.getByAccountId(PersonDaoImpl.java:23)
at org.example.playground.ldap.spring.Main.main(Main.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Caused by: java.lang.NullPointerException
at org.springframework.ldap.core.support.AbstractContextSource.getReadWriteContext(AbstractContextSource.java:175)
at org.springframework.ldap.pool.factory.DirContextPoolableObjectFactory.makeObject(DirContextPoolableObjectFactory.java:149)
at org.apache.commons.pool.impl.GenericKeyedObjectPool.borrowObject(GenericKeyedObjectPool.java:1220)
at org.springframework.ldap.pool.factory.PoolingContextSource.getContext(PoolingContextSource.java:443)
... 17 more

回答1:

Solution 1 - based on the answer from here

@Bean
public ContextSource ldapContextSource() {
    LdapContextSource contextSource = new LdapContextSource();
    contextSource.setUrl(ldapUrl);
    contextSource.setBase(ldapBase);
    contextSource.setUserDn(ldapUsername);
    contextSource.setPassword(ldapPassword);
    contextSource.afterPropertiesSet(); // *** need this ***

    PoolingContextSource poolingContextSource = new PoolingContextSource();
    poolingContextSource.setDirContextValidator(new DefaultDirContextValidator());
    poolingContextSource.setContextSource(contextSource);
    poolingContextSource.setTestOnBorrow(true);
    poolingContextSource.setTestWhileIdle(true);

    TransactionAwareContextSourceProxy proxy = new TransactionAwareContextSourceProxy(poolingContextSource);

    return proxy;
}

Solution 2 - separate the creation of LdapContextSource and PoolingContextSource, the spring container will take care the lifecycle of the beans (i.e. afterPropertiesSet())

@Bean
public LdapContextSource ldapContextSource() {
    LdapContextSource contextSource = new LdapContextSource();
    contextSource.setUrl(ldapUrl);
    contextSource.setBase(ldapBase);
    contextSource.setUserDn(ldapUsername);
    contextSource.setPassword(ldapPassword);
    return contextSource;
}

@Bean
public ContextSource poolingLdapContextSource() {
    PoolingContextSource poolingContextSource = new PoolingContextSource();
    poolingContextSource.setDirContextValidator(new DefaultDirContextValidator());
    poolingContextSource.setContextSource(ldapContextSource());
    poolingContextSource.setTestOnBorrow(true);
    poolingContextSource.setTestWhileIdle(true);

    TransactionAwareContextSourceProxy proxy = new TransactionAwareContextSourceProxy(poolingContextSource);
    return proxy;
}