我需要在现有代码库中添加第二个LdapTemplate。 LDAP目录完全不同,什么也不共享。
其他人似乎也有类似的需求:https://github.com/spring-projects/spring-ldap/issues/409。我找到了Multiple LDAP repositories with Spring LDAP Repository,并添加了第二组LDAP配置条目作为spring.ldap2.
和这个附加的Config类:
@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抱怨:
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
我试图添加@Primary
,但是也可以将其添加到我的课程中,并且所有现有代码已经使用“旧”主模板。不幸的是,没有@Second
。
编辑
我做了更多尝试:
@Autowired
@Qualifier("ldapTemplate1")
LdapTemplate ad;
@Autowired
@Qualifier("ldapTemplate2")
LdapTemplate gd;
第一个bean:
@Bean(name="ldapTemplate1")
public LdapTemplate ldapTemplate(@Qualifier("contextSource1") LdapContextSource contextSource) {
return new LdapTemplate(contextSource);
}
第二个豆:
@Bean(name = "ldapTemplate2")
public LdapTemplate ldapTemplate(@Qualifier("contextSource2") LdapContextSource contextSource) {
return new LdapTemplate(contextSource);
}
然后Spring Boot对此抱怨:
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'
即使我将两者中的一个重命名为LdapTemplate
,Spring Boot也无法自动接线。
如何自动配置两个独立的LdapTemplate
?我正在使用Spring Boot 2.0.5
答案 0 :(得分:0)
这是我在使用Spring 4和Spring LDAP的Tomcat 8.5上部署的应用程序中使用的方法。
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>
适应您的需求和/或转换为Java配置。
这里是依赖类,几乎和您一样:
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));
}
}