当前位置: 首页 > 知识库问答 >
问题:

Spring LDAP PoolingContextSource通过注释

靳举
2023-03-14

我正在尝试将Spring LDAP 池上下文源 XML 配置转换为使用注释。我能够通过遵循此处提到的一个来使 Ldap 上下文源正常工作,但我无法使池上下文源正常工作。当我运行代码时,我得到了空点异常。下面列出了 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

共有2个答案

薛墨一
2023-03-14

可以使用以下代码在中激活池(https://docs . spring . io/spring-LDAP/docs/1 . 3 . 2 . release/reference/html/Pooling . html)

  • SpringBoot应用程序
  • 使用Spring Data与Ldap服务器通信

缺少的重要部分是将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;
  }

}

萧展鹏
2023-03-14

解决方案1——基于这里的答案

@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容器将负责beans的生命周期(即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;
}
 类似资料:
  • 我是Spring Security的新手。我看过很多关于如何通过外部属性文件的注释注入值的文章。我尝试了很多方法,但最终都是用java。lang.IllegalArgumentException:无法解析占位符“val.id”异常。 你能给我一些提示如何处理这个例外吗? 我的java类如下所示: 我的属性文件名为val.properties,位于WEB-INF下,其内容为val.id=xyz 我将

  • 我在整个代码中有一堆常量,用于系统的各种可调整属性。我正在将它们全部移动到一个中央文件中。我目前的解决方案是有一个静态加载文件并公开各种getter方法,如下所示: 唯一的问题是,对于我从这个文件中获得的每个常量,我都有一些样板: 我不认为我想使用Spring或类似的东西,因为那看起来更像是boilerplae。我希望使用自定义注释来解决这个问题。我找到了这个教程,但是我真的不能弄清楚如何从注释处

  • 问题内容: 我使用我的uuid如下: 但是我收到了一个聪明的Hibernate警告: 使用org.hibernate.id.UUIDHexGenerator不会生成符合IETF RFC 4122的UUID值;考虑改用org.hibernate.id.UUIDGenerator 所以我想切换到,现在我的问题是如何将其告知Hibernate的生成器。我看到有人用它作为“hibernateuuid”-这

  • 如何使用注释在Spring中提供bean继承?在XML配置中,我使用了

  • 问题内容: 有什么方法可以通过JPA注释指定SQL注释?表和列的注释。 问题答案: 有什么方法可以通过JPA注释指定SQL注释?表和列的注释。 否。如果要定义表和列注释,最好的选择是在生成的DDL中根据事实进行操作,然后再对数据库执行操作。

  • 我想从AOP连接点检索一个注释。我能够通过反射获得注释,但无法通过ProcedingJoinPoint获得注释。 我的注释方法 我的绒球