0

I need to add a second LdapTemplate to an existing code base. The LDAP directories are totally different and share nothing.

Others seem to have similar needs: https://github.com/spring-projects/spring-ldap/issues/409. I found Multiple LDAP repositories with Spring LDAP Repository and added a second set of LDAP configuration entries as spring.ldap2. and this additional Config class:

@Configuration
public class LdapConfig {
    @Autowired
    private Environment environment;

    @Bean(name = "ldapProperties2")
    @ConfigurationProperties(prefix = "spring.ldap2")
    public LdapProperties ldapProperties() {
        return new LdapProperties();
    }

    @Bean(name = "contextSource2")
    public LdapContextSource contextSourceTarget(@Qualifier("ldapProperties2") LdapProperties ldapProperties) {
        LdapContextSource source = new LdapContextSource();
        source.setUserDn(ldapProperties.getUsername());
        source.setPassword(ldapProperties.getPassword());
        source.setBase(ldapProperties.getBase());
        source.setUrls(ldapProperties.determineUrls(this.environment));
        source.setBaseEnvironmentProperties(
                Collections.<String, Object>unmodifiableMap(ldapProperties.getBaseEnvironment()));
        return source;
    }

    @Bean(name = "ldapTemplate2")
    public LdapTemplate ldapTemplate(@Qualifier("contextSource2") LdapContextSource contextSource) {
        return new LdapTemplate(contextSource);
    }
}

Boot complains:

Parameter 0 of constructor in org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration required a single bean, but 2 were found:
    - ldapProperties2: defined by method 'ldapProperties' in class path resource [my/company/foo/LdapConfig.class]
    - spring.ldap-org.springframework.boot.autoconfigure.ldap.LdapProperties: defined in null

I tried to add @Primary but this can also be added to my class and all existing code already uses the "old" primary template. Unfortunately there is no @Second.

EDIT

I made more attempts:

@Autowired
@Qualifier("ldapTemplate1")
LdapTemplate ad;

@Autowired
@Qualifier("ldapTemplate2")
LdapTemplate gd;

First bean:

@Bean(name="ldapTemplate1")
public LdapTemplate ldapTemplate(@Qualifier("contextSource1") LdapContextSource contextSource) {
    return new LdapTemplate(contextSource);
}

Second bean:

@Bean(name = "ldapTemplate2")
    public LdapTemplate ldapTemplate(@Qualifier("contextSource2") LdapContextSource contextSource) {
        return new LdapTemplate(contextSource);
    }

Then Spring Boot complains about this:

Field ad in com.example... required a bean of type 'org.springframework.ldap.core.LdapTemplate' that could not be found.
    - Bean method 'ldapTemplate' in 'LdapDataAutoConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.ldap.core.LdapOperations; SearchStrategy: all) found beans of type 'org.springframework.ldap.core.LdapOperations' ldapTemplate2
    - User-defined bean method 'ldapTemplate' in 'LdapConfiguration2'

Even when I rename on of the two to LdapTemplate Spring Boot is unable to autowire.

How can I autoconfigure two independent LdapTemplates ? I am using Spring Boot 2.0.5

Marged
  • 10,577
  • 10
  • 57
  • 99

1 Answers1

0

This is the approach I have used in an app deployed on Tomcat 8.5 with Spring 4 and Spring LDAP.

beans.xml:

<beans:bean id="contextSource-one"
    class="org.springframework.ldap.pool2.factory.PooledContextSource">
    <beans:constructor-arg>
        <beans:bean class="org.springframework.ldap.pool2.factory.PoolConfig"
        p:testOnBorrow="true" p:minEvictableIdleTimeMillis="300000" p:maxWaitMillis="5000" />
    </beans:constructor-arg>
    <beans:property name="contextSource">
        <beans:bean
            class="...ContextSource"...>
        </beans:bean>
    </beans:property>
    <beans:property name="dirContextValidator">
        <beans:bean
            class="org.springframework.ldap.pool2.validation.DefaultDirContextValidator" />
    </beans:property>
</beans:bean>

<beans:bean id="contextSource-two"
    class="org.springframework.ldap.pool2.factory.PooledContextSource">
    <beans:constructor-arg>
        <beans:bean class="org.springframework.ldap.pool2.factory.PoolConfig"
        p:testOnBorrow="true" p:minEvictableIdleTimeMillis="300000" p:maxWaitMillis="5000" />
    </beans:constructor-arg>
    <beans:property name="contextSource">
        <beans:bean
            class="...ContextSource"...>
        </beans:bean>
    </beans:property>
    <beans:property name="dirContextValidator">
        <beans:bean
            class="org.springframework.ldap.pool2.validation.DefaultDirContextValidator" />
    </beans:property>
</beans:bean>

Adapt to your needs and/or convert to Java config.

Here is the dependent class, almost like yours:

public class MyClass {
    private List<LdapTemplate> ldapTemplates;

    @Autowired
    public MyClass(List<ContextSource> contextSources) {
        this.ldapTemplates = new LinkedList<>();
        for (ContextSource cs : contextSources)
            this.ldapTemplates.add(new LdapTemplate(cs));
    }
}
Michael-O
  • 18,123
  • 6
  • 55
  • 121