title: Spring LDAP 2.0.2 - No Injection (or how to inject) - @Autowired
body: <p>I am new to Spring LDAP (the version I am using is: 2.0.2) and I have some questions. I am querying Active Directory.</p>
  
  <p>I am going to post my code, and my questions are in my main class - <strong>MainLdapTest</strong>:</p>
  
  <p><strong>ldap_config.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
      &lt;!-- Information about this configuration file can be found:
              http://docs.spring.io/spring-ldap/docs/current/reference/#configuration
      --&gt;
      &lt;ldap:context-source id="contextSource"
                           url="ldap://&lt;url&gt;:&lt;port&gt;"
                           base="DC=comp,DC=com"
                           username="&lt;username&gt;"
                           password="&lt;secret&gt;" /&gt;
  
      &lt;ldap:ldap-template id="ldapTemplate" context-source-ref="contextSource" /&gt;
  
      &lt;bean id="LdapUserDAO" class="com.ldap.dao.LdapUserDAOImpl"&gt;
          &lt;property name="ldapTemplate" ref="ldapTemplate" /&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>LdapUser.java</strong></p>
  
  <pre><code>@Entry(objectClasses = {"top", "person", "organizationalPerson", "user"})
  public final class LdapUser {
      @Id
      private Name distinguishedName;
  
      @Attribute(name="cn")
      private String cn;
  
      @Attribute(name="description")
      private String description;
  
      @Attribute(name="displayName")
      private String displayName;
  
      @Attribute(name="sAMAccountName")
      private String sAMAccountName;
  
      @Attribute(name="mail")
      private String mail;
  
      @Attribute(name="userPrincipalName")
      private String userPrincipalName;
  
      @Attribute(name="userAccountControl")
      private String userAccountControl;
      // ...
      // getters and setters
      // ...
  </code></pre>
  
  <p><strong>LdapUserDAO.java</strong></p>
  
  <pre><code>public interface LdapUserDAO
  {
      public LdapUser create(LdapUser ldapUser);
      public void update(LdapUser ldapUser);
      public void delete(LdapUser ldapUser);
      public LdapUser findByUid(String uid);
      public List&lt;LdapUser&gt; findAll();
      public List&lt;LdapUser&gt; findByLastName(String lastName);
      public List&lt;LdapUser&gt; findBySAMAccountName(String sAMAccountName);
      public List&lt;LdapUser&gt; findDisabledUsers();
      public List&lt;LdapUser&gt; findDisabledUser(LdapUser ldapUser);
      public List&lt;LdapUser&gt; findByObjectClassPerson();
      public void setLdapTemplate(LdapTemplate ldapTemplate);
  }
  </code></pre>
  
  <p><strong>LdapUserDAOImpl.java</strong></p>
  
  <pre><code>public class LdapUserDAOImpl implements LdapUserDAO
  {
      @Autowired
      private LdapTemplate localLdapTemplate;
      // ...
      public List&lt;LdapUser&gt; findBySAMAccountName(String sAMAccountName) {
          return localLdapTemplate.find(query().where("sAMAccountName").is(sAMAccountName), LdapUser.class);
      }
      // ...
  </code></pre>
  
  <p><strong>MainLdapTest.java</strong></p>
  
  <pre><code>public class MainLdapTest
  {
      @Autowired
      private LdapUserDAO ldapUserDao;
  
      public MainLdapTest() {
          /**
           * The ldap_config.xml above is in my classpath, 
           * but how do I specify that the contents of that file must be used to inject ldapTemplate in this class, or any other class?
           *
           * Below are my tests and their result:
           *
           * Tests
           * 1. This test returns a NullPointerException because ldapTemplate is not set.
           *    Shouldn't it be injected by the "@Autowired" annotation?
           */
          List&lt;LdapUser&gt; queryListError = ldapUserDao.findByObjectClassPerson();
          System.out.println("[ERR] Query returns no values[queryListRetValues]: #[" + queryListError.size() + "]");
  
          /**
           * 2. As I set the LdapContextSource manually, it returns the data searched in the query.
           *
           *    sAMAccountName is the &lt;username&gt;
           */
          LdapContextSource ctxSrc = new LdapContextSource();
          ctxSrc.setUrl("&lt;url&gt;");
          ctxSrc.setBase("&lt;base&gt;");
          ctxSrc.setUserDn("&lt;username&gt;");
          ctxSrc.setPassword("&lt;password&gt;");
          ctxSrc.afterPropertiesSet();
  
          ldapUserDao = new LdapUserDAOImpl(ldapTemplate);
          List&lt;LdapUser&gt; queryListRetValues = ldapUserDao.findBySAMAccountName("&lt;username&gt;");
          System.out.println("[OK] Query returns no values[queryListRetValues]: #[" + queryListRetValues.size() + "]");
      }
  
      public static void main(String[] args)
      {
          MainLdapTest t = new MainLdapTest();
      }
  }
  </code></pre>
  
  <p>In fact, my code is based on the one in <strong>Spring LDAP - ODM [1]</strong>. I have used the <strong>"@Autowired"</strong> annotation and as I understand the LdapTemplate should be injected into my LdapUserDAOImpl.</p>
  
  <p>I know there is a basic error here, I just can't find where.</p>
  
  <p>As I was not sure if I should only post the link, but I have asked the same question in the spring forum [2].</p>
  
  <p>Links:
  <a href="http://docs.spring.io/spring-ldap/docs/2.0.2.RELEASE/reference/#odm" rel="nofollow" title="Spring Ldap Reference">[1-Spring Ldap Reference]</a>
  <a href="http://forum.spring.io/forum/spring-projects/data/ldap/749814-spring-ldap-2-0-2-no-injection-or-how-to-inject-autowired" rel="nofollow" title="Spring Forum Question">[2-Spring Forum Question]</a></p>
  
  <p>Thanks for your attention and help.</p>
  
  <p>Regards!</p>