这需要我在周末学到的知识,并将其作为我自己问题的答案。我仍然不是这方面的专家,所以我欢迎更有经验的答案或 cmets。
解释
首先,我仍然不确定是否需要@EnableLdapRepositories 注释。我还没有使用这些功能,所以我不能说它是否重要,或者 Spring Boot 2 是否仍在自动处理这些问题。我怀疑 Spring Boot 2 是,但我不确定。
其次,Spring Boot 的自动配置都发生在任何用户配置之后,例如我的代码配置了第二个 LDAP 数据源。根据上下文源或 LdapTemplate 的存在,自动配置使用几个条件注释来确定它是否运行。
这意味着它看到了我的“第二个”LDAP 上下文源(条件只是存在一个上下文源 bean,不管它的名称是什么或它使用什么属性)并跳过自己创建一个,这意味着我没有不再配置我的主要数据源。
它还会看到我的“第二个”LdapTemplate(同样,条件是存在一个 LdapTemplate bean,不管它的名称是什么,或者它使用的是什么上下文源或属性)并跳过自己创建一个,所以我再次不再配置我的主要数据源。
不幸的是,这些条件意味着在这种情况下,两者都没有中间(例如,我可以手动配置上下文源,然后允许 LdapTemplate 的自动配置仍然发生)。所以解决方案是让我的配置在自动配置之后运行,或者根本不利用自动配置并自己设置它们。
至于让我的配置在自动配置之后运行:唯一的方法是让我的配置本身成为自动配置,并指定它的顺序在 Spring 的内置自动配置之后(参见:https://stackoverflow.com/a/53474188/3669288)。这不适合我的用例,所以对于我的情况(因为 Spring Boot 的设置对于标准的单源情况确实有意义)我坚持放弃自动配置并自己设置它们。
代码
以下两个答案很好地涵盖了设置两个数据源(尽管部分原因是其他原因),正如我的问题中所链接的那样,但我也会在此处详细说明我的设置。
Can a spring ldap repository project access two different ldap directories?
Multiple LDAP repositories with Spring LDAP Repository
首先,需要创建配置类,因为以前在 Spring Boot 2 中根本不需要。同样,我省略了 @EnableLdapRepositories 注释,部分原因是我还没有使用它,部分原因是我认为 Spring Boot 2 仍然会为我解决这个问题。 (注意:所有这些代码都是在 Stack Overflow 答案框中输入的,因为我没有编写此代码的开发环境,因此跳过了导入,并且代码可能无法完美编译和正常运行,尽管我希望一切顺利。)
@Configuration
public class LdapConfiguration {
}
二是手动配置主数据源;曾经被自动配置但不再是的那个。这里可以利用 Spring Boot 的一个自动配置,即读取标准的 spring.ldap.* 属性(到属性对象中),但由于它没有给出名称,因此您必须通过以下方式引用它其完全限定的类名。这意味着您可以直接跳到为主数据源设置上下文源。此代码不像实际的自动配置代码那样功能齐全(请参阅:Spring Code)
我将此 LdapTemplate 标记为@Primary,因为对于我的使用,这是主要数据源,因此它是所有其他自动装配调用的默认值。这也意味着您不需要 @Qualifier 来自动连接此源(如下所示)。
@Configuration
public class LdapConfiguration {
@Bean(name="contextSource")
public LdapContextSource ldapContextSource(@Qualifier("spring.ldap-org.springframework.boot.autoconfigure.ldap.LdapProperties") LdapProperties properties) {
LdapContextSource source = new LdapContextSource();
source.setUrls(properties.getUrls());
source.setUserDn(properties.getUsername());
source.setPassword(properties.getPassword());
source.setBaseEnvironmentProperties(Collections.unmodifiableMap(properties.getBaseEnvironment()));
return source;
}
@Bean(name="ldapTemplate")
@Primary
public LdapTemplate ldapTemplate(@Qualifier("contextSource") LdapContextSource source) {
return new LdapTemplate(source);
}
}
第三是手动配置辅助数据源,即导致所有这一切开始的那个。对于这一点,您确实需要将属性读取配置到 LdapProperties 对象中。此代码建立在前面的代码之上,因此您可以看到完整的上下文类。
@Configuration
public class LdapConfiguration {
@Bean(name="contextSource")
public LdapContextSource ldapContextSource(@Qualifier("spring.ldap-org.springframework.boot.autoconfigure.ldap.LdapProperties") LdapProperties properties) {
LdapContextSource source = new LdapContextSource();
source.setUrls(properties.getUrls());
source.setUserDn(properties.getUsername());
source.setPassword(properties.getPassword());
source.setBaseEnvironmentProperties(Collections.unmodifiableMap(properties.getBaseEnvironment()));
return source;
}
@Bean(name="ldapTemplate")
@Primary
public LdapTemplate ldapTemplate(@Qualifier("contextSource") LdapContextSource source) {
return new LdapTemplate(source);
}
@Bean(name="ldapProperties2")
@ConfigurationProperties("app.ldap2")
public LdapProperties ldapProperties2() {
return new LdapProperties();
}
@Bean(name="contextSource2")
public LdapContextSource ldapContextSource2(@Qualifier("ldapProperties2") LdapProperties properties) {
LdapContextSource source = new LdapContextSource();
source.setUrls(properties.getUrls());
source.setUserDn(properties.getUsername());
source.setPassword(properties.getPassword());
source.setBaseEnvironmentProperties(Collections.unmodifiableMap(properties.getBaseEnvironment()));
return source;
}
@Bean(name="ldapTemplate2")
public LdapTemplate ldapTemplate2(@Qualifier("contextSource2") LdapContextSource source) {
return new LdapTemplate(source);
}
}
最后,在使用这些 LdapTemplates 的类中,您可以像往常一样自动装配它们。这使用构造函数自动装配而不是字段自动装配使用的其他两个答案。尽管建议使用构造函数自动装配,但两者在技术上都是有效的。
@Component
public class LdapProcessing {
protected LdapTemplate ldapTemplate;
protected LdapTemplate ldapTemplate2;
@Autowired
public LdapProcessing(LdapTemplate ldapTemplate, @Qualifier("ldapTemplate2") LdapTemplate ldapTemplate2) {
this.ldapTemplate = ldapTemplate;
this.ldapTemplate2 = ldapTemplate2;
}
}
TLDR:定义“第二个”LDAP 数据源会停止第一个 LDAP 数据源的自动配置,因此如果使用多个,则必须(几乎完全)手动配置两者;即使是第一个 LDAP 数据源,也无法利用 Spring 的自动配置。