使用Spring Java Config配置嵌入式ApacheDS服务器

时间:2015-08-05 15:16:01

标签: java spring apacheds

所以我有一个现有的配置文件来配置org.springframework.ldap.core.ContextSource

@Configuration
public class LdapContextConfig {

    @Inject
    private LdapProperties ldapProperties;

    @Bean
    public ContextSource contextSource() {
        final TransactionAwareContextSourceProxy transactionAwareContextSourceProxy =
                new TransactionAwareContextSourceProxy(poolingContextSource());
        return transactionAwareContextSourceProxy;
    }

    @Bean
    public PoolingContextSource poolingContextSource() {
        final PoolingContextSource poolingContextSource = new MutablePoolingContextSource();
        poolingContextSource.setContextSource(ldapContextSource());
        poolingContextSource.setDirContextValidator(new DefaultDirContextValidator());
        poolingContextSource.setTestOnBorrow(true); //default false
        return poolingContextSource;
    }

    @Bean
    public LdapContextSource ldapContextSource() {
        final LdapContextSource ldapContextSource = new LdapContextSource();
        ldapContextSource.setUrl(ldapProperties.getUrl());
        ldapContextSource.setBase(ldapProperties.getBase());
        ldapContextSource.setUserDn(ldapProperties.getUserDn());
        ldapContextSource.setPassword(ldapProperties.getPassword());
        return ldapContextSource;
    }

}

这使用自定义LdapProperties类在部署应用程序时从属性文件中提取url,base,userDn和password。

现在,我正在尝试设置嵌入式ApacheDS服务器以用于集成测试。

我有另一个配置文件来设置ApacheDS DirectoryService

@Configuration
public class EmbeddedLdapConfig {

    @Bean
    public DirectoryService getDirectoryService() throws Exception {
        final DirectoryService directoryService = new DefaultDirectoryService();

        //other config

        return directoryService;
    }
}

现在我正在寻找一种方法将ApacheDS DirectoryService映射到Spring LdapContextSource

我已将DirectoryService注入我的LdapContextConfig,并为嵌入式@Bean创建了单独的LdapContextSource

@Inject
private DirectoryService directoryService;

@Bean
public LdapContextSource embeddedLdapContextSource() {
    final LdapContextSource embeddedLdapContextSource = /*convert DirectoryService to LdapContextSource*/;
    return embeddedLdapContextSource;
}

是否有可能以某种方式从ApacheDS LdapContextSource获得DirectoryService?如果是这样,我将如何去做呢?

0 个答案:

没有答案
相关问题