我试图将Spring LDAP PoolingContextSource XML配置转换为使用注释。我可以按照提及here的方式使LdapContextSource工作,但我无法使PoolingContextSource工作。当我运行代码时,我得到了NullPointerException。下面列出了XML,注释和异常片段。
XML配置代码段
<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"/>
注释配置代码段
@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 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
答案 0 :(得分:2)
解决方案1 - 基于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;
}
解决方案2 - 分离LdapContextSource和PoolingContextSource的创建,spring容器将关注bean的生命周期(即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;
}
答案 1 :(得分:0)
可以使用以下代码来激活a中的池(https://docs.spring.io/spring-ldap/docs/1.3.2.RELEASE/reference/html/pooling.html)
在那里缺少的重要部分-是将PoolingContextSource
传递到LdapTemplate
请参见
中的new LdapTemplate(poolingLdapContextSource());
@PropertySource("ldap-${spring.profiles.active}.properties")
@Configuration
public class LdapConfiguration {
private static Logger LOG = LoggerFactory.getLogger(LdapConfiguration.class);
@Autowired
private Environment env;
@Bean
public LdapContextSource contextSource() {
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl(env.getRequiredProperty("ldap.url"));
contextSource.setBase(env.getRequiredProperty("ldap.base"));
contextSource.setUserDn(env.getRequiredProperty("ldap.user"));
contextSource.setPassword(env.getRequiredProperty("ldap.password"));
contextSource.setPooled(false); // Robert improved the performance by 100x for PARALLEL requests by enabling that
Map<String, Object> environment = new HashMap<>();
// DEBUGGING
environment.put("com.sun.jndi.ldap.connect.pool.debug", "fine"); // many debug infos
contextSource.setBaseEnvironmentProperties(environment);
return contextSource;
}
/**
* Configure the LDAP connection pool to
* validate connections https://docs.spring.io/spring-ldap/docs/1.3.2.RELEASE/reference/html/pooling.html
*
* so that the connections do not expire in pool
*
* @return
*/
@Bean
public ContextSource poolingLdapContextSource() {
PoolingContextSource poolingContextSource = new PoolingContextSource();
poolingContextSource.setDirContextValidator(new DefaultDirContextValidator());
poolingContextSource.setContextSource(contextSource());
poolingContextSource.setTestOnBorrow(true);
poolingContextSource.setTestWhileIdle(true);
poolingContextSource.setTimeBetweenEvictionRunsMillis(60000);
poolingContextSource.setNumTestsPerEvictionRun(3);
TransactionAwareContextSourceProxy proxy = new TransactionAwareContextSourceProxy(poolingContextSource);
return proxy;
}
@Bean
public LdapTemplate ldapTemplate() {
LdapTemplate ldapTemplate = new LdapTemplate(poolingLdapContextSource());
ldapTemplate.setIgnoreNameNotFoundException(true);
return ldapTemplate;
}
}