用SpringJava配置共同配置嵌入式ApacheDS服务器

用SpringJava配置共同配置嵌入式ApacheDS服务器,java,spring,apacheds,Java,Spring,Apacheds,因此,我有一个现有的配置文件,用于配置org.springframework.ldap.core.ContextSource @Configuration public class LdapContextConfig { @Inject private LdapProperties ldapProperties; @Bean public ContextSource contextSource() { final TransactionAwar

因此,我有一个现有的配置文件,用于配置
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;
    }

}
@Configuration
public class EmbeddedLdapConfig {

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

        //other config

        return directoryService;
    }
}
@Inject
private DirectoryService directoryService;

@Bean
public LdapContextSource embeddedLdapContextSource() {
    final LdapContextSource embeddedLdapContextSource = /*convert DirectoryService to LdapContextSource*/;
    return embeddedLdapContextSource;
}
这使用自定义的
LdapProperties
类在部署应用程序时从属性文件中提取url、base、userDn和密码

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

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

@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;
    }

}
@Configuration
public class EmbeddedLdapConfig {

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

        //other config

        return directoryService;
    }
}
@Inject
private DirectoryService directoryService;

@Bean
public LdapContextSource embeddedLdapContextSource() {
    final LdapContextSource embeddedLdapContextSource = /*convert DirectoryService to LdapContextSource*/;
    return embeddedLdapContextSource;
}
现在我正在寻找一种将ApacheDS
DirectoryService
映射到Spring
LdapContextSource
的方法

@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;
    }

}
@Configuration
public class EmbeddedLdapConfig {

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

        //other config

        return directoryService;
    }
}
@Inject
private DirectoryService directoryService;

@Bean
public LdapContextSource embeddedLdapContextSource() {
    final LdapContextSource embeddedLdapContextSource = /*convert DirectoryService to LdapContextSource*/;
    return embeddedLdapContextSource;
}
我已将
DirectoryService
注入我的
LdapContextConfig
中,并为嵌入的
LdapContextSource
创建了一个单独的
@Bean

@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;
    }

}
@Configuration
public class EmbeddedLdapConfig {

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

        //other config

        return directoryService;
    }
}
@Inject
private DirectoryService directoryService;

@Bean
public LdapContextSource embeddedLdapContextSource() {
    final LdapContextSource embeddedLdapContextSource = /*convert DirectoryService to LdapContextSource*/;
    return embeddedLdapContextSource;
}
是否可以通过某种方式从ApacheDS
DirectoryService
获取
LdapContextSource
?如果是这样的话,我该怎么做呢