Java 通过注释而不是XML配置SpringLDAPTemplate的最佳实践?
对于Spring引导应用程序,我使用注释成功地配置了SpringJava 通过注释而不是XML配置SpringLDAPTemplate的最佳实践?,java,spring-boot,annotations,spring-ldap,Java,Spring Boot,Annotations,Spring Ldap,对于Spring引导应用程序,我使用注释成功地配置了SpringLdapTemplate,包括application.properties中的LdapContextSource依赖项和@Values。(呜呜!我找不到一个例子,所以这可能会帮助其他人。) 下面的代码片段设置了上下文源,将其插入到LdapTemplate中,并将其自动连接到my DirectoryService中 在Spring Boot应用程序中设置ContextSource是否有更好/更干净的方法? application.pr
LdapTemplate
,包括application.properties中的LdapContextSource
依赖项和@Value
s。(呜呜!我找不到一个例子,所以这可能会帮助其他人。)
下面的代码片段设置了上下文源,将其插入到LdapTemplate
中,并将其自动连接到my DirectoryService中
在Spring Boot应用程序中设置ContextSource
是否有更好/更干净的方法?
application.properties(在类路径上):
MyLdapContextSource.java:
@Component
public class MyLdapContextSource extends LdapContextSource implements ContextSource {
@Value("${ldap.url}")
@Override
public void setUrl(String url) { super.setUrl(url); }
@Value("${ldap.base}")
@Override
public void setBase(String base) {super.setBase(base); }
@Value("${ldap.username}")
@Override
public void setUserDn(String userDn) {super.setUserDn(userDn); }
@Value("${ldap.password}")
@Override
public void setPassword(String password) { super.setPassword(password); }
}
MyLdapTemplate.java:
@Component
public class MyLdapTemplate extends LdapTemplate {
@Autowired
public MyLdapTemplate(ContextSource contextSource) { super(contextSource); }
}
DirectoryService.java:
@Service
public class DirectoryService {
private final LdapTemplate ldapTemplate;
@Value("${ldap.base}")
private String BASE_DN;
@Autowired
public DirectoryService(LdapTemplate ldapTemplate) { this.ldapTemplate = ldapTemplate; }
public Person lookupPerson(String username) {
return (Person) ldapTemplate.lookup("cn=" + username, new PersonAttributesMapper());
}
public List<Person> searchDirectory(String searchterm) {
SearchControls searchControls = new SearchControls();
searchControls.setCountLimit(25);
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
List<Person> people = (List<Person>) ldapTemplate.search(
BASE_DN, "cn=" + searchterm, searchControls, new PersonAttributesMapper());
return people;
}
}
@服务
公共类目录服务{
私人最终LdapTemplate LdapTemplate;
@值(“${ldap.base}”)
私有字符串基DN;
@自动连线
公共目录服务(LdapTemplate LdapTemplate){this.LdapTemplate=LdapTemplate;}
公众人物名称(字符串用户名){
return(Person)ldapTemplate.lookup(“cn=“+username,newPersonattributesMapper());
}
公共列表搜索目录(字符串搜索术语){
SearchControls SearchControls=新的SearchControls();
searchControls.setCountLimit(25);
searchControls.setSearchScope(searchControls.SUBTREE_范围);
列表人员=(列表)ldapTemplate.search(
BASE_DN,“cn=“+searchterm,searchControls,new PersonalAttributesMapper());
还人,;
}
}
为什么所有的子类都是?只需使用配置来配置bean。XML或Java配置
@Configuration
public class LdapConfiguration {
@Autowired
Environment env;
@Bean
public LdapContextSource contextSource () {
LdapContextSource contextSource= new LdapContextSource();
contextSource.setUrl(env.getRequiredProperty("ldap.url"));
contextSource.setBase(env.getRequiredProperty("ldap.base"));
contextSource.setUserDn(env.getRequiredProperty("ldap.user"));
contextSource.setPassword(env.getRequiredProperty("ldap.password"));
return contextSource;
}
@Bean
public LdapTemplate ldapTemplate() {
return new LdapTemplate(contextSource());
}
}
您的DirectoryService
可以保持不变,因为它将自动连接LdapTemplate
一般的经验法则是,您不希望扩展基础结构bean(如
DataSource
或LdapTemplate
),而是显式地配置它们。这与您的应用程序bean(服务、存储库等)相反。对于直接的情况,根本不需要显式地连接LDAP。这正是Spring Boot首先通过固执己见来消除的目标
确保您的pom:xml
中包含了springbootstarter数据ldap
或springldap核心
依赖项,例如对于Maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
然后简单地依靠Spring来自动连线,例如使用现场注入1:
参考:
1通常情况下,现场注射是必要的,但为了简洁起见,这里使用现场注射。为什么?为什么所有的子类化?为什么不简单地在xml或java配置中配置它们呢?对我来说,为只是配置而进行子类化似乎有些过分…谢谢你的建议,M!(也就是说,很明显,“为什么”的答案是b/c我正在学习,而且它起了作用,但这就是为什么我把它放出来进行审查,并提出更好/更干净的建议。更友好的说法可能是,“为了获得更好/更干净的方法,请创建一个配置类,其中包含@configuration和返回所需bean类型的方法,这些bean类型使用环境来访问Spring引导属性,而不是@Value。"--我将试用代码并报告。再次感谢,M-您的建议起到了作用,减少了LoC,并帮助我更好地理解基于注释的配置样式。从2014年起,对这个Q进行了很棒的更新。多年来发生了很多变化。下次我使用LDAP时,将使用这些信息。Thx!这对我今天关于康涅狄格州的问题有帮助非常感谢您!您知道如何使用上述方法加载两个LDAP服务器配置吗?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
# Note the spring prefix for each and use just the CN for username
spring.ldap.url=ldap://server.domain.com:389
spring.ldap.base=OU=Employees,OU=Users,DC=domain,DC=com
spring.ldap.username=myuserid
spring.ldap.password=secretthingy
@Autowired
private final LdapTemplate ldapTemplate;