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 LdapTemplate
s ? I am using Spring Boot 2.0.5