通过注释,而不是XML配置Spring LdapTemplate最佳实践? [英] Best practice for configuring Spring LdapTemplate via annotations instead of XML?

查看:1298
本文介绍了通过注释,而不是XML配置Spring LdapTemplate最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于春季启动应用程序,我成功地配置使用的注释的,包括一个春天 LdapTemplate LdapContextSource 依赖与 @Value 期从application.properties。 (活泉!我无法找到一个例子,也许这会帮助别人。)

的片段(如下图)设置上下文源,其注入的 LdapTemplate 和自动装配该到我了DirectoryService。

有没有设置一个更好/更清洁的方式好的ContextSource 在Spring应用程序启动?

application.properties(在classpath中):

 与ldap.URL = LDAP://server.domain.com:389
ldap.base:OU =员工,OU =用户,DC =域,DC = COM
ldap.username:CN = myuserid,OU =雇员,OU =用户,DC =域,DC = COM
ldap.password:secretthingy

MyLdapContextSource.java:

  @Component
公共类MyLdapContextSource扩展LdapContextSource实现好的ContextSource {    @Value($ {与ldap.URL})
    @覆盖
    公共无效setUrl(字符串URL){super.setUrl(URL); }    @Value($ {ldap.base})
    @覆盖
    公共无效setBase(字符串基地){super.setBase(基地); }    @Value($ {ldap.username})
    @覆盖
    公共无效setUserDn(字符串用户DN){super.setUserDn(用户DN); }    @Value($ {ldap.password})
    @覆盖
    公共无效setPassword(字符串密码){super.setPassword(密码); }
}

MyLdapTemplate.java:

  @Component
公共类MyLdapTemplate扩展LdapTemplate {    @Autowired
    公共MyLdapTemplate(好的ContextSource好的ContextSource){超(好的ContextSource); }
}

DirectoryService.java:

  @Service
公共类DirectoryService中{    私人最终LdapTemplate ldapTemplate;    @Value($ {ldap.base})
    私人字符串BASE_DN;    @Autowired
    公共DirectoryService中(LdapTemplate ldapTemplate){this.ldapTemplate = ldapTemplate; }    公众人物lookupPerson(字符串username){
        回报(人)ldapTemplate.lookup(CN =+用户名,新PersonAttributesMapper());
    }    公开名单<&人GT; sea​​rchDirectory(字符串搜索关键词){
        SearchControls searchControls =新SearchControls();
        sea​​rchControls.setCountLimit(25);
        sea​​rchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);        清单<&人GT;人=(列表<&人GT;)ldapTemplate.search(
                BASE_DN,CN =+搜索关键词,searchControls,新PersonAttributesMapper());
        返回的人;
    }
}


解决方案

为什么所有的子类?只需使用配置中配置的豆子。无论是XML或Java配置。

  @Configuration
公共类LdapConfiguration {    @Autowired
    环境Env;    @豆
    公共LdapContextSource好的ContextSource(){
        LdapContextSource好的ContextSource =新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));
        返回好的ContextSource;
    }    @豆
    公共LdapTemplate ldapTemplate(){
        返回新LdapTemplate(好的ContextSource());
    }}

DirectoryService中可以保持不变,因为这将有 LdapTemplate 自动装配。

根据经验,一般的规则是,你不希望延长你的基础设施豆类(如数据源 LdapTemplate ),但明确地配置它们。这相对于你的应用程序豆(服务,仓库等等)。

For a Spring Boot application, I successfully configured a Spring LdapTemplate using annotations, including the LdapContextSource dependency with @Values from application.properties. (Woot! I couldn't find an example, so maybe this will help others.)

The snippets (below) setup the context source, inject it into an LdapTemplate, and autowire that into my DirectoryService.

Is there a better/cleaner way to setup the ContextSource in a Spring Boot app?

application.properties (on the classpath):

ldap.url=ldap://server.domain.com:389
ldap.base:OU=Employees,OU=Users,DC=domain,DC=com
ldap.username:CN=myuserid,OU=employees,OU=Users,DC=domain,DC=com
ldap.password:secretthingy

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;
    }
}

解决方案

Why all the subclasses? Just use configuration to configure the beans. Either XML or Java Config.

@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());        
    }

}

Your DirectoryService can remain the same as it will have the LdapTemplate autowired.

A general rule of thumb is that you don't want to extend your infrastructure beans (like DataSource or LdapTemplate) but configure them explicitly. This as opposed to your application beans (services, repositories etc.).

这篇关于通过注释,而不是XML配置Spring LdapTemplate最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆