简体   繁体   English

Spring不加载LDAP-XML配置文件

[英]Spring does not load LDAP-XML config file

I have a prototype spring application to play with spring-security and LDAP. 我有一个Spring应用程序原型,可与spring-security和LDAP一起使用。 The application works with the internal LDAP-Server. 该应用程序可与内部LDAP服务器一起使用。 But when I want to define an individual connection with a xml-config it doesn't work. 但是,当我想用​​xml-config定义单个连接时,它不起作用。 More specific: The LdapTemplate which should be instantiated by the xml-config stays a null-object. 更具体地说:应该由xml-config实例化的LdapTemplate保留为空对象。 Here is the code: 这是代码:

    public class UserRepo {

    @Autowired
    private LdapTemplate ldapTemplate;  //stays null
    public static final String BASE_DN = "dc=springframework,dc=org";

    //this works, but is not desired:
    public UserRepo() {
//      final GenericXmlApplicationContext appContext = new GenericXmlApplicationContext("classpath:ldap.xml");
//      appContext.refresh();
//      ldapTemplate = (LdapTemplate)appContext.getBean(LdapTemplate.class);
//      LdapContextSource lcs = new LdapContextSource();
//      lcs.setUrl("ldap://127.0.0.1:389/");
//      lcs.setUserDn(BASE_DN);
//      lcs.setDirObjectFactory(DefaultDirObjectFactory.class);
//      lcs.setAnonymousReadOnly(true);
//      lcs.afterPropertiesSet();
//      ldapTemplate = new LdapTemplate(lcs);
    }

My config looks like this: 我的配置如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ldap="http://www.springframework.org/schema/ldap"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">

   <ldap:context-source
          id="contextSource"
          url="ldap://localhost:389"
          base="dc=example,dc=com"
          username="cn=Manager"
          password="secret" />

   <ldap:ldap-template id="ldapTemplate" />

   <bean id="userRepo" class="user.UserRepo">
      <property name="ldapTemplate" ref="ldapTemplate" />
   </bean>

</beans>

I have put the ldap.xml one time in the src-dir and another time in the ressource-dir, both didn't work. 我已经将ldap.xml一次放在src目录中,另一次放在了ressource目录中,两者都没有用。

I hope you can tell me what I'm doing wrong. 我希望你能告诉我我在做什么错。

Add a @Component annotation to your UserRepo class and a proper annotation-config (or component-scan element if required). 将@Component批注添加到UserRepo类,并添加适当的批注配置(如果需要,也可以添加component-scan元素)。 Since your commented-out code works, the LdapTemplate bean is created succesfully, and injected because you use a GenericXmlApplicationContext. 由于注释掉的代码有效,因此LdapTemplate bean成功创建并由于使用GenericXmlApplicationContext而被注入。 To test it with annotations you need an AnnotationConfigApplicationContext instead. 要使用批注测试它,您需要一个AnnotationConfigApplicationContext。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM